Filename scanning for certain names Python [duplicate] - python

This question already has answers here:
Find all files in a directory with extension .txt in Python
(25 answers)
Closed 7 years ago.
I have lots of files in a directory, lets say around 100, most of their file names begin with "Mod", i need to add all filenames that begin with "Mod" to a list so i can reference them later in my code. Any help? Thanks!

Use the glob package.
import glob
filepaths = glob.glob('/path/to/file/Mod*')
More generally, you can use os.listdir. Unlike glob, it only returns the last part of the filename (without the full path).
import os
directory = '/path/to/directory'
filenames = os.listdir(directory )
full_filepaths = [os.path.join(directory, f) for f in filenames]
only_files = [f for f in full_filepaths if os.path.isfile(f)]

You can use glob library to find the files with the given pattern:
import glob,os
mylist=[]
os.chdir("/mydir")
for file in glob.glob("Mod*"):
mylist.append(file)
print mylist
or you can use os.walk
for root, dirs, files in os.walk('/mydir'):
for names in files:
if names.startswith("Mod"):
mylist.append(os.path.join(root, names))

Related

Rename all files that match with a regex [duplicate]

This question already has answers here:
Python renaming file with regex
(6 answers)
Closed 19 days ago.
I have the files in following format:
abc-h-25-Data.db
abc-h-25-Filter.db
abc-h-25-Index.db
abc-h-25-Metadata.db
abc-h-25-Statistics.db
abc-h-25-Summary.db
Need to rename by replacing the h by j as follows:
abc-j-25-Data.db
abc-j-25-Filter.db
abc-j-25-Index.db
abc-j-25-Metadata.db
abc-j-25-Statistics.db
abc-j-25-Summary.db
Any bash/python script that can automate this for me? Thanks!
i write basic script to rename file names according to your requirement and if you want any help for the same comment here
import os
path = "folder-path"
for root, dirs, files in os.walk(path):
for filename in files:
if "abc-h-25-" in filename:
os.rename(root+filename, root+filename.replace("abc-h-25-", "abc-j-25-" ))
print(filename)
You can use the following command to automate this task:
for file in abc-h-*; do mv "$file" "${file/h/j}"; done
Here's a bash script that performs the same task as the above command, but with descriptive variable names:
#!/bin/bash
old_prefix="abc-h-"
new_prefix="abc-j-"
for filename in "${old_prefix}"*; do
new_filename="${filename/$old_prefix/$new_prefix}"
mv "$filename" "$new_filename"
done
Here's a Python script that performs the same task as the previous bash scripts:
import os
old_prefix = "abc-h-"
new_prefix = "abc-j-"
for filename in os.listdir():
if filename.startswith(old_prefix):
new_filename = filename.replace(old_prefix, new_prefix)
os.rename(filename, new_filename)
You can use os.rename() function and glob function to rename the files with specific patterns in specific paths. My approach to your problem is as follows.
import os
import glob
# Absolute Path of folder with files.
files_path = r"C:\Users\User1\Documents\GitHub\files"
# Regex Pattern to filter specific files in folder.
regex_pattern = "abc-h*.db"
all_files = glob.glob(f'{files_path}\{regex_pattern}')
for i in all_files:
os.rename(i, i.replace("h", "j"))
print("Sucessful!")

get filenames in a directory without extension - Python [duplicate]

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"))

How to get list of subdirectories names [duplicate]

This question already has answers here:
How to get all of the immediate subdirectories in Python
(15 answers)
Closed 7 years ago.
There is a directory that contains folders as well as files of different formats.
import os
my_list = os.listdir('My_directory')
will return full content of files and folders names. I can use, for example, endswith('.txt') method to select just text files names, but how to get list of just folders names?
I usually check for directories, while assembling a list in one go. Assuming that there is a directory called foo, that I would like to check for sub-directories:
import os
output = [dI for dI in os.listdir('foo') if os.path.isdir(os.path.join('foo',dI))]
You can use os.walk() in various ways
(1) to get the relative paths of subdirectories. Note that '.' is the same value you get from os.getcwd()
for i,j,y in os.walk('.'):
print(i)
(2) to get the full paths of subdirectories
for root, dirs, files in os.walk('path'):
print(root)
(3) to get a list of subdirectories folder names
dir_list = []
for root, dirs, files in os.walk(path):
dir_list.extend(dirs)
print(dir_list)
(4) Another way is glob module (see this answer)
Just use os.path.isdir on the results returned by os.listdir, as in:
def listdirs(path):
return [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
That should work :
my_dirs = [d for d in os.listdir('My_directory') if os.path.isdir(os.path.join('My_directory', d))]
os.walk already splits files and folders up into different lists, and works recursively:
for root,dirs,_ in os.walk('.'):
for d in dirs:
print os.path.join(root,d)

How to get only files in directory? [duplicate]

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 9 years ago.
I have this code:
allFiles = os.listdir(myPath)
for module in allFiles:
if 'Module' in module: #if the word module is in the filename
dirToScreens = os.path.join(myPath, module)
allSreens = os.listdir(dirToScreens)
Now, all works well, I just need to change the line
allSreens = os.listdir(dirToScreens)
to get a list of just files, not folders.
Therefore, when I use
allScreens [ f for f in os.listdir(dirToScreens) if os.isfile(join(dirToScreens, f)) ]
it says
module object has no attribute isfile
NOTE: I am using Python 2.7
You can use os.path.isfile method:
import os
from os import path
files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]
Or if you feel functional :D
files = filter(path.isfile, os.listdir(dirToScreens))
"If you need a list of filenames that all have a certain extension, prefix, or any common string in the middle, use glob instead of writing code to scan the directory contents yourself"
import os
import glob
[name for name in glob.glob(os.path.join(path,'*.*')) if os.path.isfile(os.path.join(path,name))]

Reading all files in all directories [duplicate]

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 9 years ago.
I have the code working to read in the values of a single text file but am having difficulties reading all files from all directories and putting all of the contents together.
Here is what I have:
filename = '*'
filesuffix = '*'
location = os.path.join('Test', filename + "." + filesuffix)
Document = filename
thedictionary = {}
with open(location) as f:
file_contents = f.read().lower().split(' ') # split line on spaces to make a list
for position, item in enumerate(file_contents):
if item in thedictionary:
thedictionary[item].append(position)
else:
thedictionary[item] = [position]
wordlist = (thedictionary, Document)
#print wordlist
#print thedictionary
note that I am trying to stick the wildcard * in for the filename as well as the wildcard for the filesuffix. I get the following error:
"IOError: [Errno 2] No such file or directory: 'Test/.'"
I am not sure if this is even the right way to do it but it seems that if I somehow get the wildcards working - it should work.
I have gotten this example to work: Python - reading files from directory file not found in subdirectory (which is there)
Which is a little different - but don't know how to update it to read all files. I am thinking that in this initial set of code:
previous_dir = os.getcwd()
os.chdir('testfilefolder')
#add something here?
for filename in os.listdir('.'):
That I would need to add something where I have an outer for loop but don't quite know what to put in it..
Any thoughts?
Python doesn't support wildcards directly in filenames to the open() call. You'll need to use the glob module instead to load files from a single level of subdirectories, or use os.walk() to walk an arbitrary directory structure.
Opening all text files in all subdirectories, one level deep:
import glob
for filename in glob.iglob(os.path.join('Test', '*', '*.txt')):
with open(filename) as f:
# one file open, handle it, next loop will present you with a new file.
Opening all text files in an arbitrary nesting of directories:
import os
import fnmatch
for dirpath, dirs, files in os.walk('Test'):
for filename in fnmatch.filter(files, '*.txt'):
with open(os.path.join(dirpath, filename)):
# one file open, handle it, next loop will present you with a new file.

Categories

Resources