split filename on name and extenstion in python [duplicate] - python

This question already has answers here:
Extracting extension from filename in Python
(33 answers)
Getting file extension using pattern matching in python
(6 answers)
Closed 5 years ago.
I have this pattern:
dir1/dir2/.log.gz
dir1/dir2/a.log.gz
dir1/dir2/a.py
dir1/dir2/*.gzip.tar
I want to get filename or path and extension. e.g:
(name,extension)=(dir1/dir2/,.log.gz)
(name,extension)=(dir1/dir2/a,.log.gz)
(name,extension)=(dir1/dir2/a,.py)
(name,extension)=(dir1/dir2/,.gzip.tar)
I try:
re.findall(r'(.*).*\.?(.*)',path)
but it doesn't work perfect

If you just want the file's name and extension:
import os
# path = C:/Users/Me/some_file.tar.gz
temp = os.path.splitext(path)
var = (os.path.basename(temp[0]), temp[1])
print (var)
# (some_file.tar, .gz)
Its worth noting that files with "dual" extensions will need to be recursed if you want. For example, the .tar.gz is a gzip file that happens to be an archive file as well. But the current state of it is .gz.
There is more on this topic here on SO.

General strategy: find the first '.' everything before it is the path, everything after it is the extension.
def get_path_and_extension(filename):
index = filename.find('.')
return filename[:index], filename[index + 1:]

Related

delete images containing specific word in its name using python [duplicate]

This question already has answers here:
How do i search directories and find files that match regex?
(4 answers)
Closed 8 months ago.
In the directory there are multiple images with names:
I want to delete all images with "340" in it using a python code.
Assuming that the images are in desktop/images/
I'm not sure why you need to use Python and can't just use your shell (in bash it would just be rm desktop/images/*340*)
But in Python I think the shortest way would be
import os, glob
for file in glob.glob("desktop/images/*340*"):
os.remove(file)
or even:
import os, glob
[os.remove(file) for file in glob.glob("desktop/images/*340*")]
You can use this example, you'll have to adjust the code for your correct directory:
import os
import re
for file in os.listdir("Desktop/Images"):
if re.search("340.*?\.jpg$", file):
os.remove("Desktop/Images/" + file)
print("Deleted " + file)
else:
print("No match for " + file)

Find the name of a file except its directory [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 1 year ago.
I used the following code to read all files in a folder:
files_path = [os.path.abspath(x) for x in os.listdir()]
fnames_transfer = [x for x in files_path if x.endswith(".mkv")]
Now I need to extract the name of the file except its directory. For example fnames_transfer is E:\pythoncode\feature\1.mkv. I need to extract 1.mkv from this string. What should I do for this?
I used this os.path.basename(fnames_transfer[i] )and it works for me.

Using files in directory that are matching with a list [duplicate]

This question already has answers here:
Get a filtered list of files in a directory
(14 answers)
Closed 2 years ago.
I have a directory with a lot of audio files and a list that contains the names of some of these files. I want to process only the files in the directory that are matching with my name list.
My attempt (that is not working) is this:
path_to_audio_files = 'D:/Data_small/output_conv/'
sample_list = ['s1.wav', 's2.wav', 's3.wav']
import fnmatch
for file in os.listdir(path_to_audio_files):
if fnmatch.fnmatch(file, sample_list):
fs, signal = wavfile.read(file)
Is this even the right way to do it or how can it be done?
Thanks!
Why use fnmatch (I have never heard of it) instead of python native capabilities? (in operator)
path_to_audio_files = 'D:/Data_small/output_conv/'
sample_list = ['s1.wav', 's2.wav', 's3.wav']
for file in os.listdir(path_to_audio_files):
if file in sample_list:
fs, signal = wavfile.read(file)

How to remove extension from multiple files in python [duplicate]

This question already has an answer here:
Batch File Rename with Python
(1 answer)
Closed 2 years ago.
I have a folder which have more than 100 files. The folder contains.txt files. i want to remove ".txt" extension from all files. also tried os.path.splitext even os.remove it doesn't work.
You can use os.rename(). The file name is just a string. You can remove the 4 last characters with:
src = 'path_to_txt.txt'
dst = src[:-4]
os.rename(src, dst)
You can use stem from the pathlib module.
import pathlib
file_name = pathlib.Path(‘textfile.txt‘)
file_name_wo_ext = file_name.stem

How to search files with multiple extensions in a specific path using python [duplicate]

This question already has answers here:
Python glob multiple filetypes
(40 answers)
Closed 5 years ago.
I have written a small python code to read specific files containing .txt from the specific path. I would like to do the same thing to search for files with multiple extensions more or less with the same code with little modifications. Ofcourse, I am not looking for / which will search all the extensions. Any help would be appreciated.
varlogpath = "C:/Users/vveldand/Office/Fetcher/Projects/LOG-PARSER/var/log/*.txt"
outputfile = open(wrfilename, "a")
files=glob.glob(varlogpath)
you could do something like this:
files=None
# put file extensions into a list
fileext=[".txt",".log",".csv"]
for ext in fileext:
varlogpath = "C:/Users/vveldand/Office/Fetcher/Projects/LOG-
PARSER/var/log/*"+ext
outputfile = open(wrfilename, "a")
files=glob.glob(varlogpath)
wrfilename = os.path.join(wrscriptpath, 'TiC_Timeline_Report.txt')
varlogpath = "C:/Users/vveldand/Office/Fetcher/Projects/LOG-PARSER/var/log/*.txt"
outputfile = open(wrfilename, "a")
files=[glob.glob(varlogpath.replace(".txt",ext)) for ext in list_of_extensions_you_want_to_search]

Categories

Resources