This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 1 year ago.
I am trying to make a code in Python where when it runs it will search a directory and its sub-directories for files ending with a file extension ".pdm". I want to note this is not on a personal computer but on a cloud provider. The variable current_dur is just a starting point to narrow down the search. The directory has to go at through at least 3 more folders in its sub-directories.
import os
current_dur = r'\\dmn1.MIR.com\MIRFILE\FS159\FIRSCODB\IR Data Modeling\PIR\IR - Information Report'
for item in os.listdir(current_dur):
if not os.path.isfile(item):
for file in os.listdir(item):
if file.endswith('.pdm'):
print(file)
It returns the error message [WinError 3] The system cannot find the path specified: 'Archive'
Have you tried using os.walk()?
import os
current_dur = r'\\dmn1.MIR.com\MIRFILE\FS159\FIRSCODB\IR Data Modeling\PIR\IR - Information Report'
pdm_files = []
for root, dirs, files in os.walk(current_dur):
for file in files:
if file.endswith('.pdm'):
pdm_files.append(os.path.join(root, file))
Related
This question already has answers here:
Delete multiple files matching a pattern
(9 answers)
Closed 6 months ago.
To better illustrate what I'm trying to do, I download a iTunes file in m4a but it's encrypted by widevine so I decrypt it to get a DRM free m4a file
so in the download folder there is:
8 - Bring Me The Horizon - Run.m4a
8 - Bring Me The Horizon - Run_encrypted.m4a
And my question is : is there a sort of algorithm who can filter files in my download folder with the word "encrypted" and delete them?
You can do this using glob.
import glob
import os
# path to search file
path = 'path_to_folder/*encrypted.m4a'
for file_path in glob.glob(path, recursive=True):
print(file_path)
os.remove(file_path)
This question already has answers here:
Using os.walk() to recursively traverse directories in Python
(14 answers)
Closed 2 years ago.
i have a dataset and one folder and in this folder i have a few subfolder in them there is audio file i need to move on the whole subfolders and get the file and the path , i using python
osomeone has idea?
folder: dataset -> folders: rock,regge,hiphop,classic,disco,jazz,pop,blues,country,metal -> files: "name".wav
i need to enter each folder and get the file and path.
i have 100 files in folder.
i dont try nothing because i dont know how to do that
You should use os.walk
import os
for subdir, dirs, files in os.walk(rootdir):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".wav"):
print(filepath)
This question already has answers here:
How to rename a file using Python
(17 answers)
Closed 3 years ago.
I have a root folder with several folders and files and I need to use Python to rename all matching correspondences. For example, I want to rename files and folders that contain the word "test" and replace with "earth"
I'm using Ubuntu Server 18.04. I already tried some codes. But I'll leave the last one I tried. I think this is really easy to do but I don't have almost any knowledge in py and this is the only solution I have currently.
import os
def replace(fpath, test, earth):
for path, subdirs, files in os.walk(fpath):
for name in files:
if(test.lower() in name.lower()):
os.rename(os.path.join(path,name), os.path.join(path,
name.lower().replace(test,earth)))
Is expected to go through all files and folders and change the name from test to earth
Here's some working code for you:
def replace(fpath):
filenames = os.listdir()
os.chdir(fpath)
for file in filenames:
if '.' not in file:
replace(file)
os.rename(file, file.replace('test', 'earth'))
Here's an explanation of the code:
First we get a list of the filenames in the directory
After that we switch to the desired folder
Then we iterate through the filenames
The program will try to replace any instances of 'test' in each filename with 'earth'
Then it will rename the files with 'test' in the name to the version with 'test' replaced
If the file it is currently iterating over is a folder, it runs the function again with the new folder, but after that is done it will revert back to the original
Edited to add recursive iteration through subfolders.
This question already has answers here:
How to find newest file with .MP3 extension in directory?
(6 answers)
Closed last year.
I have many files in a folder. Like:
tb_exec_ns_decile_20190129.csv
tb_exec_ns_decile_20190229.csv
tb_exec_ns_decile_20190329.csv
So i just want to pick latest file:
tb_exec_ns_decile_20190329.csv
import glob
import os
latest_csv = max(glob.glob('/path/to/folder/*.csv'), key=os.path.getctime) #give path to your desired file path
print latest_csv
Since your csv files share a common prefix, you can
simply use max on the list of files. Assuming you are located
in the directory with your files and tb_exec_ns_decile_20190329.csv
has the latest date:
>>> import glob
>>> max(glob.glob('tb_exec_ns_decile_*.csv'))
'tb_exec_ns_decile_20190329.csv'
This question already has answers here:
Find all files in a directory with extension .txt in Python
(25 answers)
Closed 6 years ago.
The following python code counts the number of total files I have in a directory which contains multiple subdirectories. The result prints the subdirectory name along with the number of files it contains.
How can I modify this so that:
It only looks for a specific file extension (i.e. "*.shp")
It provides both the number of ".shp" files in each subdirectory and a final count of all ".shp" files
Here is the code:
import os
path = 'path/to/directory'
folders = ([name for name in os.listdir(path)])
for folder in folders:
contents = os.listdir(os.path.join(path,folder))
print(folder,len(contents))
you can use the .endswith() function on strings. This is handy for recognizing extensions. You could loop through the contents to find these files then as follows.
targets = []
for i in contents:
if i.endswith(extension):
targets.append(i)
print(folder, len(contents))
Thanks for the comments and answer, this is the code I used (feel free to flag my question as a duplicate of the linked question if it is too close):
import os
path = 'path/to/directory'
folders = ([name for name in os.listdir(path)])
targets = []
for folder in folders:
contents = os.listdir(os.path.join(path,folder))
for i in contents:
if i.endswith('.shp'):
targets.append(i)
print(folder, len(contents))
print "Total number of files = " + str(len(targets))