how to include folders in python code - python

Let's say two folders. One -X and one more-Y inside X.Now lets say I've set my working path to folder X inside ATOM IDE and now if I want use the folder Y in my code how do I do it?
for example while writing below code I'm inside folder X so
import glob2
import datetime
filenames = glob2.glob('*.txt')
#How do I list files of folder Y only???
with open(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")+".txt", 'w') as file:
#How do I create file inside folder Y only
for item in filenames:
with open(item,"r") as f:
content = f.read()
file.write(content)
file.write("\n")

You can use below code to switching around different directory
Path='path to y'
currentDir = os.getcwd()
os.chdir(Path)
#do your job here
#now come back to previous directory
os.chdir(currentDir)

Let's take your directory structure :
x/
some_script.py
y/
Now what you're looking for is to create a file inside y by writing some code in some_script.py
This is how you do it :
fh = open('y/a.txt', 'w')
fh.write("Yayy")
fh.close()

Related

How to run python file from subfolder so current folder file path automatically detect and edit data?

Y.py python file location "D:\\Folder\\Sub Folder\\Y.py"
1.html file location "D:\\Folder\\1.html"
every time Folder name change python code outFileName has to edit.There is any way Folder path catch automatically.
python code is
import os
import glob
import re
html = ""
strPath = os.path.realpath(__file__)
print( f"Full Path :{strPath}" )
nmFolders = strPath.split( os.path.sep )
print( f"Folder Name :{nmFolders[-2]}" )
for X in {nmFolders[-2]}:
for file in sorted(glob.glob( os.path.join('*.html') )):
for Y in {'*.webp', '*.png', '*.jpg'}:
for image in sorted(glob.glob( os.path.join(Y))[:1]):
for X in {nmFolders[-2]}:
html += f'''<div class="card"><img class="card__image" src="{os.path.realpath(image)}" width="200px alt=""><div class="card__content"><p1>{X}</p1></div><div class="card__info"></div></div>'''
for X in {nmFolders[-2]}:
outFileName = "D:\\Folder\\1.html"
# Read the contents of the HTML file into a string
with open(outFileName, "r", encoding="utf-8") as f:
existing_html = f.read()
if html not in existing_html:
# Write the new HTML to the file
with open(outFileName, "a", encoding="utf-8") as f:
f.write(html + '\n')
so what is changing? The parent folder that contains
Y.py and 1.html?
what is the "Folder Path" you are trying to catch automatically? Sharing your code doesn't clarify what you are trying to do in this situation.
Assuming you are trying to catch D:\\Folder, you can traverse the current directory you're in using the .. in your file path.
so if you current directory (aka current folder) is
D:\\Folder\\Sub Folder\\Y.py
you can get to
D:\\Folder\\1.html
by using
..\\1.html
so basically two dots: .., means "go up one folder from where I currently am"

How to write lists in files with python

My data is organized as such:
I have 30 folders. In each of them, 3 subfolders. In each of them, one file.
I would like to write a script that writes, in a text file 1 located in folder 1, the paths to the files located in the subfolders of this folder 1; and so on for every other folder.
The problem is that the script only writes, in each text file, the 3rd file (file in subfolder 3) rather than the files in subfolders 1, 2, 3.
This is what I tried:
import glob
import os
gotofolders = '/path/to/folderslocation/'
foldersname = open('/path/to/foldersname.txt').read().split()
for folders in foldersname:
foldersdirectory = os.path.join(gotofolders,foldersname)
filepaths = glob.glob(os.path.join(foldersdirectory)+'*subfolders/*files')
for filepath in filepaths:
savethepaths = os.path.join(foldersdirectory)+'files_path_in_that_folder.txt'
with open (savethepaths,'w') as f:
f.write(filepath+'\n')
As said, it almost works, excepts that in each 'files_path_in_that_folder.txt' I have the 3rd element of the "filepath" list, rather than all 3 elements.
Thanks!
Okay, I figured it out; I had to add:
with open (savethepaths,'w') as f:
f.writelines(list("%s\n" %filepath for filepath in filepaths))
import os
def directory_into_file(_path, file_obj, depth):
# depth is a string of asterisk, just for better printing. starts with empty string
file_obj.write(depth + _path + '\n')
if(os.path.isdir(_path)):
file_list = os.listdir(_path)
os.chdir(_path)
for file in file_list:
directory_into_file(file, file_obj, depth+'*')
os.chdir("..")
this should work.
_path - the path of the directory,
file_obj - send the object file to the function and first,
depth - at first call send an empty string
hope this would work. didn't try it myself...

Reading in multiple files in directory using python

I'm trying to open each file from a directory and print the contents, so I have a code as such:
import os, sys
def printFiles(dir):
os.chdir(dir)
for f in os.listdir(dir):
myFile = open(f,'r')
lines = myFile.read()
print lines
myFile.close()
printFiles(sys.argv[1])
The program runs, but the problem here is that it is only printing one of the contents of the file, probably the last file that it has read. Does this have something to do with the open() function?
Edit: added last line that takes in sys.argv. That's the whole code, and it still only prints the last file.
There is problem with directory and file paths.
Option 1 - chdir:
def printFiles(dir):
os.chdir(dir)
for f in os.listdir('.'):
myFile = open(f,'r')
# ...
Option 2 - computing full path:
def printFiles(dir):
# no chdir here
for f in os.listdir(dir):
myFile = open(os.path.join(dir, f), 'r')
# ...
But you are combining both options - that's wrong.
This is why I prefer pathlib.Path - it's much simpler:
from pathlib import Path
def printFiles(dir):
dir = Path(dir)
for f in dir.iterdir():
myFile = f.open()
# ...
The code itself certainly should print the contents of every file.
However, if you supply a local path and not a global path it will not work.
For example, imagine you have the following folder structure:
./a
./a/x.txt
./a/y.txt
./a/a
./a/a/x.txt
If you now run
printFiles('a')
you will only get the contents of x.txt, because os.listdir will be executed from within a, and will list the contents of the internal a/a folder, which only has x.txt.

Python: Looping through files in a different directory and scanning data

I am having a hard time looping through files in a directory that is different from the directory where the script was written. I also ideally would want my script through go to through all files that start with sasa. There are a couple of files in the folder such as sasa.1, sasa.2 etc... as well as other files such as doc1.pdf, doc2.pdf
I use Python Version 2.7 with windows Powershell
Locations of Everything
1) Python Script Location ex: C:Users\user\python_project
2) Main_Directory ex: C:Users\user\Desktop\Data
3) Current_Working_Directory ex: C:Users\user\python_project
Main directory contains 100 folders (folder A, B, C, D etc..)
Each of these folders contains many files including the sasa files of interest.
Attempts at running script
For 1 file the following works:
Script is run the following way: python script1.py
file_path = 'C:Users\user\Desktop\Data\A\sasa.1
def writing_function(file_path):
with open(file_path) as file_object:
lines = file_object.readlines()
for line in lines:
print(lines)
writing_function(file_path)
However, the following does not work
Script is run the following way: python script1.py A sasa.1
import os
import sys
from os.path import join
dr = sys.argv[1]
file_name = sys.argv[2]
file_path = 'C:Users\user\Desktop\Data'
new_file_path = os.path.join(file_path, dr)
new_file_path2 = os.path.join(new_file_path, file_name)
def writing_function(paths):
with open(paths) as file_object:
lines = file_object.readlines()
for line in lines:
print(line)
writing_function(new_file_path2)
I get the following error:
with open(paths) as file_object:
IO Error: [Errno 2] No such file or directory:
'C:Users\\user\\Desktop\\A\\sasa.1'
Please note right now I am just working on one file, I want to be able to loop through all of the sasa files in the folder.
It can be something in the line of:
import os
from os.path import join
def function_exec(file):
code to execute on each file
for root, dirs, files in os.walk('path/to/your/files'): # from your argv[1]
for f in files:
filename = join(root, f)
function_exec(filename)
Avoid using the variable dir. it is a python keyword. Try print(dir(os))
dir_ = argv[1] # is preferable
No one mentioned glob so far, so:
https://docs.python.org/3/library/glob.html
I think you can solve your problem using its ** magic:
If recursive is true, the pattern “**” will match any files and zero
or more directories and subdirectories. If the pattern is followed by
an os.sep, only directories and subdirectories match.
Also note you can change directory location using
os.chdir(path)

How do I retrieve the contents of all the files in a directory in a list each one?

I would like to read the all the files in a directory so I'm doing the following:
path = '/thepath/of/the/files/*'
files = glob.glob(path)
for file in files:
print file
The problem is that when I print the files I don't obtain anything; any idea of how to return all the content of the files in a list per file?
EDIT: I appended the path with an asterisk, this should give you all the files and directories in that path.
Like in the comment I posted some time ago, this should work:
contents=[open(ii).read() for ii in glob.glob(path)]
or this, if you want a dictionary instead:
contents={ii : open(ii).read() for ii in glob.glob(path)}
I would do something like the following to only get files.
import os
import glob
path = '/thepath/of/the/files/*'
files=glob.glob(path)
for file in files:
if os.path.isfile(file):
print file
Your question is kind of unclear, but as I understand it, you'd like to get the contents of all the files in the directory. Try this:
# ...
contents = {}
for file in files:
with open(file) as f:
contents[file] = f.readlines()
print contents
This creates a dict where the key is the file name, and the value is the contents of the file.

Categories