I am trying to use glob to match all the files in a folder. All the files start with the same word but I forgot to include .txt at the end of them. Is there a way to write the code to get all of these files despite the fact there is no file extension in my directory?
I'm assuming you want to add a .txt file extension from the way you worded your question. You can use a wildcard character (*) anywhere in your glob expression
import glob
import shutil
for file in glob.glob('/path/to/files/commonWord*'):
shutil.move(file, file + '.txt')
not sure if that is what you want, but this will get all the files/folders that start with the word "california" in the same directory.
So if no folders start with "california" in your directory this will do it
import glob, os
for file_ in glob.glob('california*'):
os.rename(file_, file_+'.csv')
output1: ['california_housing_train', 'california_housing_test']
output2: ['california_housing_train.csv', 'california_housing_test.csv']
You can take them all and then check by regex whether or not it has an extension and add the file to a list or whatever you want.
import glob
import re
files = glob.glob('*')
for file in files:
if not re.findall(r'.*\.[a-z]+',file):
print(file)
Related
I know how to delete files by extension but what if my files are looking like this:
update_24-08-2022_14-54.zip.001
Where last 3 digits can be between 001-029
Here is code that I'm using for standard zip files
files_in_directory = os.listdir(directory)
filtered_files = [file for file in files_in_directory if file.endswith(".zip")]
for file in filtered_files:
path_to_file = os.path.join(directory, file)
os.remove(path_to_file)
Assuming the double extensions are of the form .zip.xyz, with xyz being triple digits, you can use globbing:
import glob
import os
for path in glob.glob('*.zip.[0-9][0-9][0-9]'):
os.remove(path)
(As a usual precaution, check first, by replacing os.remove with print).
If you have a specific directory, its name stored in directory, you can use:
import glob
import os
for path in glob.glob(os.path.join(directory, '*.zip.[0-9][0-9][0-9]')):
os.remove(path)
There is no need to join the directory and path inside the for loop (as is the case in the question): path itself will already contain the directory name.
I have a list of names(all unique) of Wav files-
2003211085_2003211078_ffc0d543799a2984c60c581d.wav
2003214817_2003214800_92720fb19bf9216c2f160733.wav
2003233142_2003233136_8c42d206701830dff6032d41.wav
2003256235_2003256218_4e71bf77b0ffb907990d2e30.wav
2003276239_2003276196_dad6aff70f37817fcd75ffb8.wav
2003352182_2003352170_b1f2990d5f867408cc39c445.wav
There is a directory called \019\Recordings where all of these files are located under various subfolders.
I want to write a python app that pulls these wav files based on their unique name from all these subfolders and places them into a single target folder.
Im new to python and tried using -
import glob, os
import shutil
target_list_of_wav_names = ["2003211085_2003211078_ffc0d543799a2984c60c581d.wav",
"2003214817_2003214800_92720fb19bf9216c2f160733.wav",
"2003233142_2003233136_8c42d206701830dff6032d41.wav"
"2003352182_2003352170_b1f2990d5f867408cc39c445.wav"]
for file in glob.glob('//19/Recordings*.wav', recursive=True):
print(file)
if file in target_list_of_wav_names:
shutil.move(file, "C:/Users/ivd/Desktop/autotranscribe"+file)
But the files do not reflect in the target folder
How can i fix this?
glob is just a utility to find files based on a wildcard. It returns the string of the files that match your query.
So you'll still need to actually move the file with another function.
you could use os.rename or shutil.move to move it
for file in glob.glob("*.wav"):
os.rename(file, f'destinationfolder/{file}')
import glob, os
import shutil
target_list_of_wav_names = ['example_wav1.wav','example_wav2.wav',...... etc]
for file in glob.glob('/019/Recordings/*.wav', recursive=True):
print(file)
if file in target_list_of_wav_names:
shutil.move(file, "/mydir/"+file)
There is an mkv file in a folder named "export". What I want to do is to make a python script which fetches the file name from that export folder.
Let's say the folder is at "C:\Users\UserName\Desktop\New_folder\export".
How do I fetch the name?
I tried using this os.path.basename and os.path.splitext .. well.. didn't work out like I expected.
os.path implements some useful functions on pathnames. But it doesn't have access to the contents of the path. For that purpose, you can use os.listdir.
The following command will give you a list of the contents of the given path:
os.listdir("C:\Users\UserName\Desktop\New_folder\export")
Now, if you just want .mkv files you can use fnmatch(This module provides support for Unix shell-style wildcards) module to get your expected file names:
import fnmatch
import os
print([f for f in os.listdir("C:\Users\UserName\Desktop\New_folder\export") if fnmatch.fnmatch(f, '*.mkv')])
Also as #Padraic Cunningham mentioned as a more pythonic way for dealing with file names you can use glob module :
map(path.basename,glob.iglob(pth+"*.mkv"))
You can use glob:
from glob import glob
pth ="C:/Users/UserName/Desktop/New_folder/export/"
print(glob(pth+"*.mkv"))
path+"*.mkv" will match all the files ending with .mkv.
To just get the basenames you can use map or a list comp with iglob:
from glob import iglob
print(list(map(path.basename,iglob(pth+"*.mkv"))))
print([path.basename(f) for f in iglob(pth+"*.mkv")])
iglob returns an iterator so you don't build a list for no reason.
I assume you're basically asking how to list files in a given directory. What you want is:
import os
print os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
If there's multiple files and you want the one(s) that have a .mkv end you could do:
import os
files = os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
mkv_files = [_ for _ in files if _[-4:] == ".mkv"]
print mkv_files
If you are searching for recursive folder search, this method will help you to get filename using os.walk, also you can get those file's path and directory using this below code.
import os, fnmatch
for path, dirs, files in os.walk(os.path.abspath(r"C:/Users/UserName/Desktop/New_folder/export/")):
for filename in fnmatch.filter(files, "*.mkv"):
print(filename)
You can use glob
import glob
for file in glob.glob('C:\Users\UserName\Desktop\New_folder\export\*.mkv'):
print(str(file).split('\')[-1])
This will list out all the files having extention .mkv as
file.mkv, file2.mkv and so on.
From os.walk you can read file paths as a list
files = [ file_path for _, _, file_path in os.walk(DIRECTORY_PATH)]
for file_name in files[0]: #note that it has list of lists
print(file_name)
Hello I'm new to python and I'd like to know how to process a .txt file line by line to copy files specifid as wild cards
basically the .txt file looks like this.
bin/
bin/*.txt
bin/*.exe
obj/*.obj
document
binaries
so now with that information I'd like to be able to read my .txt file match the directory copy all the files that start with * for that directory, also I'd like to be able to copy the folders listed in the .txt file. What's the best practical way of doing this? your help is appreciated, thanks.
Here's something to start with...
import glob # For specifying pathnames with wildcards
import shutil # For doing common "shell-like" operations.
import os # For dealing with pathnames
# Grab all the pathnames of all the files matching those specified in `text_file.txt`
matching_pathnames = []
for line in open('text_file.txt','r'):
matching_pathnames += glob.glob(line)
# Copy all the matched files to the same filename + '.new' at the end
for pathname in matching_pathnames:
shutil.copyfile(pathname, '%s.new' % (pathname,))
You might want to look at the glob and re modules
http://docs.python.org/library/glob.html
Given a local directory structure of /foo/bar, and assuming that a given path contains exactly one file (filename and content does not matter), what is a reasonably fast way to get the filename of that single file (NOT the file content)?
1st element of os.listdir()
import os
os.listdir('/foo/bar')[0]
Well I know this code works...
for file in os.listdir('.'):
#do something
you can also use glob
import glob
print glob.glob("/path/*")[0]
os.path.basename will return the file name for you
so you can use it for the exact one file by adding your file path :
os.path.basename("/foo/bar/file.file")
or you can run through the files in the folder and read all names
file_src = "/foo/bar/"
for x in os.listdir(file_src):
print(os.path.basename(x))