I'm new to Python. I have to open a folder which contains several folders. Then inside each folder of the several folders there is one folder contains the files that i want to read. i need to open the folder then loop through the several folders inside then open the folder in each of them then open the files. i don't really know how to start. can anyone please help me where to start.
i'm trying the following but got really stuck
path_to_json = r'main folder'
for file_name in os.listdir(path_to_json):
if file_name.endswith(".json"):
print(file_name)
else:
current_path = "".join((path_to_json, "/", file_name))
if os.path.isdir(current_path):
scan_folder(current_path)
Walk is a good way to do this:
for root_dir, _, file_names in os.walk(starting_directory):
for file_name in file_names:
if file_name.endswith('.json'):
print(f'{root_dir}/{file_name}')
Related
I can work around it by explicitly mentioning the file names in a list and skip the loop if the file is found in the list. But is there any explicit function or a way to do this?
My folder has a lot of folders and a bunch of individual files (shortcuts,.pptx, .xlsx, .docx,...) the folder directory.
You can check using os.path.isdir. When you're looping over all of the files in the directory, simply write:
for f in os.listdir(path):
if os.path.isdir(f):
do_something()
This should probably work
import os
path = "/home/you/Documents"
dir = os.listdir(path)
for i in range(len(dir)):
if not os.path.isdir(path+""+dir[i]):
print(dir[i])
I'm trying to make a script that iterates through folders in a directory and saves the very first one in the folder to a variable. I'm trying to do this because I have folders in a directory with hundreads of .exr files but I only need the first file in the folder to be saved to the directory. I can get it to print out all the files in each folder but thats a bit too much info than I need. Is there an easy way to do this using something like os.walk? This is what I'm working with so far
import os
def main():
dirName = r"F:\FOLDERNAME"
#Get the List of all files in the directory tree at given path
listOfFiles = getListOfFiles(dirName)
#Print the files
for elem in listOfFiles:
print(elem)
if __name__ =='__main__':
main()
Thanks yall!
If you already:
can get it to print out all the files in each folder
And you only want :
the very first one
Then all you need is something like:
first_file = list_of_files[0]
Looks like you're still learning the very basics of coding. I suggest looking up lessons/tutorials on lists, list indexing, and iterables.
If you use os.walk(), it returns a list of files in each directory. You can print the first one.
for root, dirs, files in os.walk(dirName):
if files:
print(os.path.join(root, files[0]))
Say I have a directory.
In this directory there are single files as well as folders.
Some of those folders could also have subfolders, etc.
What I am trying to do is find all of the files in this directory that start with "Incidences" and read each csv into a pandas data frame.
I am able to loop through all the files and get the names, but cannot read them into data frames.
I am getting the error that "___.csv" does not exist, as it might not be directly in the directory, but rather in a folder in another folder in that directory.
I have been trying the attached code.
inc_files2 = []
pop_files2 = []
for root, dirs, files in os.walk(directory):
for f in files:
if f.startswith('Incidence'):
inc_files2.append(f)
elif f.startswith('Population Count'):
pop_files2.append(f)
for file in inc_files2:
inc_frames2 = map(pd.read_csv, inc_files2)
for file in pop_files2:
pop_frames2 = map(pd.read_csv, pop_files2)
You are adding only file name to the lists, not their path. You can use something like this to add paths instead:
inc_files2.append(os.path.join(root, f))
You have to add the path from the root directory where you are
Append the entire pathname, not just the bare filename, to inc_files2.
You can use os.path.abspath(f) to read the full path of a file.
You can make use of this by making the following changes to your code.
for root, dirs, files in os.walk(directory):
for f in files:
f_abs = os.path.abspath(f)
if f.startswith('Incidence'):
inc_files2.append(f_abs)
elif f.startswith('Population Count'):
pop_files2.append(f_abs)
I'm getting lost... I'll post what I tried, but it didn't work.
So I have to get through 3 folders.
Let's say there is the main folder(label it main for this) and 100 sub folders to the main folder(1-100 labeled), but I need to get inside those subfolders(labeled A,B,C,D,E..etc for what I need, won't go more than D) and I need to read the files inside the subfolders A,B,C,D which are .txt folders.
So
Main--->1-100--->A,B,C,D for each 1-100---> read .txt folders
import os
import glob
os.chdir("/Main")
for folders in glob.glob("20*"):
print(folders)
I tried this with an extra code to get in the subfolders
for folder in folders:
glob.glob("tracking_data*")
print(folder)
Which didn't give me what I needed. The first code works fine, the 2nd code was supposed to give me a list of tracking data folders.
I know what I need to do, and am probably overcomplicating it with my lack of previous programming. Thank you
I'm not sure I understand what it is you wish to do, but assuming you want to iterate over 2-nested folders and search for .txt files, this code should do it:
if __name__ == '__main__':
import os
rootdir = '/'
txt_files = []
for _, dirs_1_100, _ in os.walk(rootdir): # iterate through 1-100
for dir_1_100 in dirs_1_100:
path_1_100 = os.path.join(rootdir, dir_1_100)
for _, dirs_A_E, _ in os.walk(path_1_100): # iterate through A-E
for dir_A_E in dirs_A_E:
path_A_E = os.path.join(path_1_100, dir_A_E)
for _, _, files in os.walk(path_A_E):
for requested_file in files:
if not requested_file.endswith('.txt'):
continue
else:
txt_files.append(os.path.join(dir_A_E, requested_file))
I'm trying to export all of my maps that are in my subdirectories.
I have the code to export, but I cannot figure out where to add the loop that will make it do this for all subdirectories. As of right now, it is exporting the maps in the directory, but not the subfolders.
import arcpy, os
arcpy.env.workspace = ws = r"C:\Users\162708\Desktop\Burn_Zones"
for subdir, dirs, files in os.walk(ws):
for file in files:
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
pdf_name = mxd[:-4] + ".pdf"
arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
del mxd_list
What am I doing wrong that it isn't able to iterate through the subfolders?
Thank you!
Iterating through os.walk result you should give tuples containing (path, dirs, files) (the first in the tuple is the current path that contains files which is why I tend to name it that way). The current directory does not change automatically so you need to incorporate it into the path you're giving to arcpy.ListFiles like this:
arcpy.ListFiles(os.path.join(path, "*.mxd"))
You should also remove the loop for file in files. It seems like you're exporting the files per directory so why export the whole directory every time for each file?
Also you should change arcpy.mapping.MapDocument(os.path.join(ws, mxd)) to arcpy.mapping.MapDocument(os.path.join(path, mxd)) where path is again the first element from os.walk.