Access folder with os - python

I want to iterate over a folder using a for-loop and os.listdir(), I'm just wondering what exactly to write into the brackets to access a particular file. Thanks!

The following example should help you:
import os
path = "C:/Users/TestDir"
dirs = os.listdir( path )
for file in dirs:
print(file)

You could easily google that. https://www.tutorialspoint.com/python/os_listdir.htm
You only need the path in the brackets.

According to what i understood, you would like to iterate over files inside a particular folder until you find one particular file? You could do something like:
import os
FileToBeFound = "Your File Name To Find"
path = "Path to the directory you would like to check for a file in"
for file in os.listdir(path):
if file == FileToBeFound:
#do stuff with file
break
else:
continue

Related

Python get all the file name in a list

The problem is to get all the file names in a list that are under a particular directory and in a particular condition.
We have a directory named "test_dir".
There, we have sub directory "sub_dir_1", "sub_dir_2", "sub_dir_3"
and inside of each sub dir, we have some files.
sub_dir_1 has files ['test.txt', 'test.wav']
sub_dir_2 has files ['test_2.txt', 'test.wav']
sub_dir_2 has files ['test_3.txt', 'test_3.tsv']
What I want to get at the end of the day is a list of of the "test.wav" that exist under the "directory" ['sub_dir_1/test.wav', 'sub_dir_2/test.wav']. As you can see the condition is to get every path of 'test.wav' under the mother directory.
mother_dir_name = "directory"
get_test_wav(mother_dir_name)
returns --> ['sub_dir_1/test.wav', 'sub_dir_2/test.wav']
EDITED
I have changed the direction of the problem.
We first have this list of file names
["sub_dir_1/test.wav","sub_dir_2/test.wav","abc.csv","abc.json","sub_dir_3/test.json"]
from this list I would like to get a list that does not contain any path that contains "test.wav" like below
["abc.csv","abc.json","sub_dir_3/test.json"]
You can use glob patterns for this. Using pathlib,
from pathlib import Path
mother_dir = Path("directory")
list(mother_dir.glob("sub_dir_*/*.wav"))
Notice that I was fairly specific about which subdirectories to check - anything starting with "sub_dir_". You can change that pattern as needed to fit your environment.
Use os.walk():
import os
def get_test_wav(folder):
found = []
for root, folders, files in os.walk(folder):
for file in files:
if file == "test.wav":
found.append(os.path.join(root, file))
return found
Or a list comprehension approach:
import os
def get_test_wav(folder):
found = [f"{arr[0]}\\test.wav" for arr in os.walk(folder) if "test.wav" in arr[2]]
return found
I think this might help you How can I search sub-folders using glob.glob module?
The main way to make a list of files in a folder (to make it callable later) is:
file_path = os.path.join(motherdirectopry, 'subdirectory')
list_files = glob.glob(file_path + "/*.wav")
just check that link to see how you can join all sub-directories in a folder.
This will also give you all the file in sub directories that only has .wav at the end:
os.chdir(motherdirectory)
glob.glob('**/*.wav', recursive=True)

Trying to print name of all csv files within a given folder

I am trying to write a program in python that loops through data from various csv files within a folder. Right now I just want to see that the program can identify the files in a folder but I am unable to have my code print the file names in my folder. This is what I have so far, and I'm not sure what my problem is. Could it be the periods in the folder names in the file path?
import glob
path = "Users/Sarah/Documents/College/Lab/SEM EDS/1.28.20 CZTS hexane/*.csv"
for fname in glob.glob(path):
print fname
No error messages are popping up but nothing will print. Does anyone know what I'm doing wrong?
Are you on a Linux-base system ? If you're not, switch the / for \\.
Is the directory you're giving the full path, from the root folder ? You might need to
specify a FULL path (drive included).
If that still fails, silly but check there actually are files in there, as your code otherwise seems fine.
This code below worked for me, and listed csv files appropriately (see the C:\\ part, could be what you're missing).
import glob
path = "C:\\Users\\xhattam\\Downloads\\TEST_FOLDER\\*.csv"
for fname in glob.glob(path):
print(fname)
The following code gets a list of files in a folder and if they have csv in them it will print the file name.
import os
path = r"C:\temp"
filesfolders = os.listdir(path)
for file in filesfolders:
if ".csv" in file:
print (file)
Note the indentation in my code. You need to be careful not to mix tabs and spaces as theses are not the same to python.
Alternatively you could use os
import os
files_list = os.listdir(path)
out_list = []
for item in files_list:
if item[-4:] == ".csv":
out_list.append(item)
print(out_list)
Are you sure you are using the correct path?
Try moving the python script in the folder when the CSV files are, and then change it to this:
import glob
path = "./*.csv"
for fname in glob.glob(path):
print fname

How to print the names of files from a folder?

I'm trying to print the names of all the files from a folder directory. I have a folder called "a", and in that folder there are 3 NC files, lets call them "b","c","d", whose directory I want to print. How would I do this?
For example, given my path to the folder is
path=r"C:\\Users\\chz08006\\Documents\\Testing\\a"
I want to print the directories to all the files in the folder "a", so the result should print:
C:\\Users\\chz08006\\Documents\\Testing\\a\\b.nc
C:\\Users\\chz08006\\Documents\\Testing\\a\\c.nc
C:\\Users\\chz08006\\Documents\\Testing\\a\\d.nc
So far, I've tried
for a in path:
print(os.path.basename(path))
But that doesn't seem to be right.
I think you're looking for this:
import os
path = r"C:\\Users\\chz08006\\Documents\\Testing\\a"
for root, dirs, files in os.walk(path):
for file in files:
print("{root}\\{file}".format(root=root, file=file))
You can have a list of file names in a folder using listdir().
import os
path = "C:\\Users\\chz08006\\Documents\\Testing\\a"
l = os.listdir(path)
for a in l:
print(path + a)
You made a couple mistakes. You were using os.path.basename, that only returns the name of the file or folder represented at the end of a path after the last file separator.
Instead, use os.path.abspath to get the full path of any file.
The other mistake was one of using the wrong variable inside the loop (print(os.path.basename(path) instead of using the variable a)
Also, dont forget to use os.listdir to list the files inside the folder before looping.
import os
path = r"C:\Users\chz08006\Documents\Testing\a"
for file in os.listdir(path): #using a better name compared to a
print(os.path.abspath(file)) #you wrote path here, instead of a.
#variable names that do not have a meaning
#make these kinds of errors easier to make,
#and harder to spot

Python - Deleting the last few characters of specific files in a directory

I'm trying to delete the last several characters of multiple files in a specific directory using the rename function. The code I have written using suggestions on this site looks like it should work, but it returns the error message:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'test1.txt' -> 'test'
And here is my code:
import os
list = os.listdir("C:\\Users\\Jonathan\\Desktop")
for file in list:
if file.startswith("test"):
os.rename(file, file[0:4])
My code shows that for all files beginning with the word "test", delete all characters after it. As I said, to me it looks like it should work, but I am new at Python, and I don't even understand what the error message means.
Are you actually in the folder where you're renaming? If not, the problem is likely that you're looking in the local folder (where you launched the program). Prepend that path to each file name:
import os
cwd = "C:\\Users\\Jonathan\\Desktop"
list = os.listdir(cwd)
for file in list:
if file.startswith("test"):
os.rename(cwd+file, cwd+"test")
As you didn't specify the complete path to your file, it is likely that your program was saving the in your root directory. Also, you should not use list or file as variable names since they shadow two of Python's types.
import os
files_path = "C:\\Users\\Jonathan\\Desktop\\"
lst = os.listdir(files_path)
for file_name in lst:
if file_name.startswith("test"):
os.rename(files_path + file_name, files_path + file_name[:-4])
Try this:
import os
list = os.listdir("C:\\Users\\Jonathan\\Desktop\\")
for file in list:
if file[:4] == "test":
os.renames(list+file, list+file[:4])
And by the way, if you need find the files and rename them recursively(That means will find all directories in that directory). You can use os.walk() like this:
for root, dirs, files in os.walk("C:\\Users\\Jonathan\\Desktop\\"):
for name in files:
if name[:4] == "test":
os.renames(os.path.join(root, name), os.path.join(root, name)[:4])
you need to use os.rename() with existing paths. if your working directory is not the directory containing the file your script will fail. this should work independently of your working directory:
files_path = "C:\\Users\\Jonathan\\Desktop\\"
lst = os.listdir(files_path)
for fle in lst:
if fle.startswith("test"):
os.rename(os.path.join(files_path, fle),
os.path.join(files_path, fle[:4]) )
and avoid using list as a varaible name.

Python filename change

I have a number of videos in a directory on my Mac that all have a specific string in the file name that I want to remove, but I want to keep the rest of the file name as it is. I'm running this python script from terminal.
I have this syntax but it doesn't seem to work. Is it practical to use the following? It seems to simple to be the best way to do this sort of thing which is why I don't think it works.
from os import rename, listdir
text = "Some text I want to remove from file name"
files = listdir("/Users/Admin/Desktop/Dir_of_videos/")
for x in files:
if text in files:
os.rename(files, files.replace(text, ""))
the problem is that you get incomplete paths when you are using listdir, basically, it returns only the files in the directory without the prepending path to the directory
this should do the job:
import os
in_dir = './test'
remove = 'hello'
paths = [os.path.join(in_dir,file) for file in os.listdir(in_dir) if remove in file]
for file in paths:
os.rename(file, file.replace(remove, ""))

Categories