I have following script which is supposed to list all picture etc from my disk drives, but it failes to find any file. Any Idea what I may be doing wrong. I am doing in PyCharm environment:
files = []
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith((".jpg", ".jpeg", ".mp3", ".mp4")):
files.append(os.path.join(root, file))
# Sort the files by their current folder name
files.sort(key=lambda x: os.path.dirname(x))
# Display a list of the files
for file in files:
print(file)
Your for-loop will never end as you add the found file name into files which you are looping through. Use another variable to store the list.
This works fine:
import os
results = []
for root, dirs, files in os.walk("."):
print(f"Got: {len(results)} files, current dir: {root}")
for file in files:
if file.endswith((".jpg", ".jpeg", ".mp3", ".mp4")):
results.append(os.path.join(root, file))
# Sort the files by their current folder name
results.sort(key=lambda x: os.path.dirname(x))
# Display a list of the files
for file in results:
print(file)
Related
I would like to know how I can only append sub-directories of a given directory to a list in Python.
Something like this is not what I'm searching for as it outputs the files:
filelist = []
for root, dirs, files in os.walk(job['config']['output_dir']):
for file in files:
# append the file name to the list
filelist.append(os.path.join(root, file))
# print all the file names
for name in filelist:
print(name)
Just use the dirs variable instead the files variable, like this:
dirs_list = []
for root, dirs, files in os.walk('test'):
for dir in dirs:
dirs_list.append(os.path.join(root, dir))
print(dirs_list)
I am trying to identify all .kml in a specific directory and save them into a new directory. Is this possible? I'm able to print the file path but I would like to use Python to copy those files to a new directory.
Here is my code so far:
import os
# traverse whole directory
for root, dirs, files in os.walk(r'C:\Users\file_path_here'):
# select file name
for file in files:
# check the extension of files
if file.endswith('.kml'):
# print whole path of files
print(os.path.join(root, file))
Try this:
import os
# traverse whole directory
for root, dirs, files in os.walk(r'C:\Users\file_path_here'):
# select file name
for each_file in files:
# check the extension of files
if each_file.endswith('.kml'):
# print whole path of files
print(os.path.join(root, file))
kml_file = open(each_file, "r")
content = kml_file.read()
file.close()
with open('newfile.kml', 'w') as f:
f.write(content)
I have a series of files that are nested as shown in the attached image. For each "inner" folder (e.g. like the 001717528 one), I want to extract a row of data from each the FITS files and create a CSV file that contains all the rows, and name that CSV file after the name of the "inner" folder (e.g. 001717528.csv that has data from the 18 fits files). The data-extracting part is easy but I have trouble coding the iteration.
I don't really know how to iterate over both the outer folders such as the 0017 and inner folders, and name the csv files as I want.
My code is looking like this:
for subdir, dirs, files in os.walk('../kepler'):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".fits"):
extract data
write to csv file
Apparently this will iterate over all files in the kepler folder so it doesn't work.
If you need to keep track of how far you've walked into the directory structure, you can count the file path delimiter (os.sep). In your case it's / because you're on a Mac.
for path, dirs, _ in os.walk("../kepler"):
if path.count(os.sep) == 2:
# path should be ../kepler/0017
for dir in dirs:
filename = dir + ".csv"
data_files = os.listdir(path + os.sep + dir)
for file in data_files:
if file.endswith(".fits"):
# Extract data
# Write to CSV file
As far as I can tell this meets your requirements, but let me know if I've missed something.
Try this code it should print the file path of all your ".fits" files:
# !/usr/bin/python
import os
base_dir = './test'
for root, dirs, files in os.walk(base_dir, topdown=False):
for name in files:
if name.endswith(".fits"):
file_path = os.path.join(root, name) #path of files
print(file_path)
# do your treatment on file_path
All you have to do is add your specific treatment.
I want to recursively traverse through all subdirs in a root folder and write all filenames to an output file. Then in each subdir, create a new output file inside the subdir and recursively traverse through its subdirs and append the filename to the new output file.
So in the example below, under the Music folder it should create a Music.m3u8 file and recursively traverse all subdirs and add all filenames in each subdir to the Music.m3u8 file. Then in Rock folder, create a Rock.m3u8 file and recursively traverse all subdirs within the Rock folder and add all filenames in each subdirs in Rock.m3u8. Finally in each Album folder, create Album1.m3u8, Album2.m3u8, etc with the filenames in its folder. How can I do this in python3.6?
Music
....Rock
........Album1
........Album2
....Hip-Hop
........Album3
........Album4
This is what I have but only adds the filenames of each folder to an output file but doesn't recursively adds to the root output file.
import os
rootdir = '/Users/bayman/Music'
ext = [".mp3", ".flac"]
for root, dirs, files in os.walk(rootdir):
path = root.split(os.sep)
if any(file.endswith(tuple(ext)) for file in files):
m3ufile = str(os.path.basename(root))+'.m3u8'
list_file_path = os.path.join(root, m3ufile)
with open(list_file_path, 'w') as list_file:
list_file.write("#EXTM3U\n")
for file in sorted(files):
if file.endswith(tuple(ext)):
list_file.write(file + '\n')
You're doing with open(list_file_path, 'w') as list_file: each time through the outer loop. But you're not creating, or writing to, any top-level file, so of course you don't get one. If you want one, you have to explicitly create it. For example:
rootdir = '/Users/bayman/Music'
ext = [".mp3", ".flac"]
with open('root.m3u', 'w') as root_file:
root_file.write("#EXTM3U\n")
for root, dirs, files in os.walk(rootdir):
path = root.split(os.sep)
if any(file.endswith(tuple(ext)) for file in files):
m3ufile = str(os.path.basename(root))+'.m3u8'
list_file_path = os.path.join(root, m3ufile)
with open(list_file_path, 'w') as list_file:
list_file.write("#EXTM3U\n")
for file in sorted(files):
if file.endswith(tuple(ext)):
root_file.write(os.path.join(root, file) + '\n')
list_file.write(file + '\n')
(I'm just guessing at what you actually want in that root file here; you presumably know the answer to that and don't have to guess…)
I am trying to use the os.walk() module to go through a number of directories and move the contents of each directory into a single "folder" (dir).
In this particular example I have hundreds of .txt files that need to be moved. I tried using shutil.move() and os.rename(), but it did not work.
import os
import shutil
current_wkd = os.getcwd()
print(current_wkd)
# make sure that these directories exist
dir_src = current_wkd
dir_dst = '.../Merged/out'
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
print(file)
#need to move files (1.txt, 2.txt, etc) to 'dir_dst'
#tried: shutil.move(file, dir_dst) = error
If there is a way to move all the contents of the directories, I would be interested in how to do that as well.
Your help is much appreciated! Thanks.
Here is the file directory and contents
current_wk == ".../Merged
In current_wkthere is:
Dir1
Dir2
Dir3..
combine.py # python script file to be executed
In each directory there are hundreds of .txtfiles.
Simple path math is required to find source files and destination files precisely.
import os
import shutil
src_dir = os.getcwd()
dst_dir = src_dir + " COMBINED"
for root, _, files in os.walk(current_cwd):
for f in files:
if f.endswith(".txt"):
full_src_path = os.path.join(src_dir, root, f)
full_dst_path = os.path.join(dst_dir, f)
os.rename(full_src_path, full_dst_path)
You have to prepare the complete path of source file, and make sure dir_dst exists.
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
shutil.move(os.path.join(root, file), dir_dst)