This question already has answers here:
Regular expression usage in glob.glob?
(4 answers)
Closed 4 years ago.
In a directory i have multiple files. but i want to fetch only few csv files with particular pattern.
Example
files in a directory: abc.csv, xyz.csv, uvw.csv, sampl.csv, code.py, commands.txt, abc_1.csv, sam.csv, xyz_1.csv, uvw_1.csv, mul.csv, pp.csv......
I need to fetch csv filenames : abc.csv , xyz.csv, uvw.csv, abc_1.csv, xyz_1.csv, uvw_1.csv, abc_2.csv , xyz_2.csv, uvw_2.csv,.... (sometimes more files with change in just the number in filename like abc_3.csv)
In python we can fetch the files using
files = glob.glob("*.csv")
But for the above requirement how to modify the above line or any other efficient way of doing it
Using Regex.
Ex:
import glob
import os
import re
for filename in glob.glob(r"Path\*.csv"):
if re.match(r"[a-z]{3}(_\d*)?\.csv", os.path.basename(filename)):
print(filename)
Related
This question already has answers here:
How to use glob() to find files recursively?
(28 answers)
Closed 17 days ago.
I have following directory structure
$ find
.
./file1.html
./soquest_glob.py
./dir1
./dir1/file2.html
./dir2
./dir2/file3.html
(I have added blank lines above to clarify files in different folders).
I am trying to find all html files (including those in subfolders) with following Python code using glob package:
$ cat soquest_glob.py
import glob
flist = glob.glob("*.html", recursive=True)
print(flist)
However, when I run this code, it finds only file in current folder, not in subfolders:
$ python3 soquest_glob.py
['file1.html']
Where is the problem and how can it be solved?
The recursive argument to glob.glob effects the behavior of **. You need to use a pattern like: glob.glob("**/*.html", recursive=True).
This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 2 years ago.
so there are alot of files with .lnk extension in the start menu folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs i want to print all those file names so i tried this code:
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive = True):
print(file)
it prints the link to the files, but i want to print only the file names with the extension of ".lnk"
Convert the absolute path to list then take the last element from the list. See the below code.
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive=True):
print(os.path.split(file)[-1])
This question already has answers here:
Python: Open file in zip without temporarily extracting it
(4 answers)
Closed 5 years ago.
I have a url link for a zip file. I want to download the zip file. Then I want to list the name of all files that are in the zip file. One of them is a .csv file. I also want to read from the csv file.
Can anybody tell me how I can do it in python3?
urllib.request.retrieve to download zip file
https://docs.python.org/3/library/urllib.request.html
zipfile module to extract files https://docs.python.org/3/library/zipfile.html
find csv file(s) in path with glob module https://docs.python.org/3/library/glob.html
finally use csv module
https://docs.python.org/3/library/csv.html
This question already has answers here:
Get a filtered list of files in a directory
(14 answers)
Closed 8 years ago.
How do you remove files given a filespec such as "*.obj" on Windows? I'm using Windows 7 and 8.1 at the moment.
Evidently os.remove does not take filespecs ("filespec" being a crude regular-expression for including wildcards such as *.txt to mean all files that end with ".txt").
The python glob module provides wildcard file matching. So
import glob
import os
for f in glob.glob("*.obj"):
os.remove(f)
This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I want to retrieve the filenames of all the files with .xml extension present in various subfolders in a single directory,
code:
import os
xmlFiles = []
for directoryPath in os.walk(filePath):
fileName = directoryPath[2]
if fileName[:3] = 'xml':# or fileName.endswith('xml'):
xmlFiles.append(fileName)
Use os.walk, and str.endswith.