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)
Related
This question already has answers here:
How do I get the filename without the extension from a path in Python?
(31 answers)
Closed 1 year ago.
I have the following:
selstim = '/Users/folder1/folder2/folder9/Pictures/Set_1/Pos/43et1.jpg'
I need to end up with:
43et1
I tried:
selstim.split('/')[-1]
Which produced:
43et1.jpg
I also tried:
selstim.split('/,.')[-1]
That doesn't get the desired result.
Is there a way to also get rid of the '.jpg' in the same line of code?
You may just find it easier to use pathlib (if you have Python 3.4+) and let it separate the path components for you:
>>> from pathlib import Path
>>> p = Path('/Users/folder1/folder2/folder9/Pictures/Set_1/Pos/43et1.jpg')
>>> p.stem
43et1
Implementation using only the standard os library.
from os import path
filePath = path.basename("/Users/folder1/folder2/folder9/Pictures/Set_1/Pos/43et1.jpg")
print(filePath) # 43et1.jpg
print(path.splitext(filePath)[0]) # 43et1, index at [1] is the file extension. (.jpg)
All in one line:
path.splitext(path.basename(FILE_PATH))[0]
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
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:
How do I get the filename without the extension from a path in Python?
(31 answers)
Closed 3 years ago.
I am trying to get the filenames in a directory without getting the extension. With my current code -
import os
path = '/Users/vivek/Desktop/buffer/xmlTest/'
files = os.listdir(path)
print (files)
I end up with an output like so:
['img_8111.jpg', 'img_8120.jpg', 'img_8127.jpg', 'img_8128.jpg', 'img_8129.jpg', 'img_8130.jpg']
However I want the output to look more like so:
['img_8111', 'img_8120', 'img_8127', 'img_8128', 'img_8129', 'img_8130']
How can I make this happen?
You can use os's splitext.
import os
path = '/Users/vivek/Desktop/buffer/xmlTest/'
files = [os.path.splitext(filename)[0] for filename in os.listdir(path)]
print (files)
Just a heads up: basename won't work for this. basename doesn't remove the extension.
Here are two options
import os
print(os.path.splitext("path_to_file")[0])
Or
from os.path import basename
print(basename("/a/b/c.txt"))
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Deleting files by type in Python on Windows
How can I delete all files with the extension ".txt" in a directory? I normally just do
import os
filepath = 'C:\directory\thefile.txt'
os.unlink(filepath)
Is there a command like os.unlink('C:\directory\*.txt') that would delete all .txt files? How can I do that?
Thanks!
#!/usr/bin/env python
import glob
import os
for i in glob.glob(u'*.txt'):
os.unlink (i)
should do the job.
Edit: You can also do it in "one line" using map operation:
#!/usr/bin/env python
import glob
import os
map(os.unlink, glob.glob(u'*.txt'))
Use the glob module to get a list of files matching the pattern and call unlink on all of them in a loop.
Iterate through all files in C:\directory\, check if the extension is .txt, unlink if yes.