How to rename image name in a folder using python? [duplicate] - python

This question already has answers here:
Rename multiple files in Python [duplicate]
(7 answers)
Closed 1 year ago.
I have over 2000 images in a folder that needs to be renamed. Currently, it's a default name as w=0&h=-_BACl339n_c4PTZDlVgaHWg9s1k_Vyz8PbhNhhXkQk=0 and I need it to name it as fear_1 and all the other images in this format. Is there any way to achieve this?

Use os.listdir to get all the filenames and os.rename to rename them
import os
path = '//path//to//folder'
files = os.listdir(path)
for index, file in enumerate(files):
os.rename(os.path.join(path, file), os.path.join(path, ''.join(['fear_',str(index+1), '.jpg'])))

Related

How to get all files in a directory? [duplicate]

This question already has answers here:
List only files in a directory?
(8 answers)
how to check if a file is a directory or regular file in python? [duplicate]
(4 answers)
Closed 2 months ago.
I have a directory and need to get all files in it, but not subdirectories.
I have found os.listdir(path) but that gets subdirectories as well.
My current temporary solution is to then filter the list to include only the things with '.' in the title (since files are expected to have extensions, .txt and such) but that is obviously not optimal.
We can create an empty list called my_files and iterate through the files in the directory. The for loop checks to see if the current iterated file is not a directory. If it is not a directory, it must be a file.
my_files = []
for i in os.listdir(path):
if not os.path.isdir(i):
my_files.append(i)
That being said, you can also check if it is a file instead of checking if it is not a directory, by using if os.path.isfile(i).
I find this approach is simpler than glob because you do not have to deal with any path joining.

Printing out all titles ending in '.txt' ( Python ) [duplicate]

This question already has answers here:
Find all files in a directory with extension .txt in Python
(25 answers)
Closed 4 years ago.
My objective is to print out all available files that end in '.txt' inside a folder, I'm unsure how to do so.
Thank you for reading.
you can use
filename.endswith('.txt')
simply just open the folder by
fileList = os.listdir("folder_path") #open that folder
for filename in fileList:
if(filename.endswith('.txt')): # check its extension
print(filename)

Creating a list of locations for files with the same name in different folders [duplicate]

This question already has answers here:
Python error os.walk IOError
(2 answers)
Closed 4 years ago.
I am trying to create a list of paths for multiple files with the same name and format from different folders. I tried doing this with os.walk with the following code:
import os
list_raster = []
for (path, dirs, files) in os.walk(r"C:\Users\Douglas\Rasters\Testing folder"):
for file in files:
if "woody02.tif" in file:
list_raster.append(files)
print (list_raster)
However, this only gives me two things
the file name
All file names in each folder
I need the the full location of only the specified 'woody02.txt' in each folder.
What am I doing wrong here?
The full path name is the first item in the tuples in the list returned by os.walk, so it is assigned to your path variable already.
Change:
list_raster.append(files)
to:
list_raster.append(os.path.join(path, file))
In the example code you posted you are appending files to your list instead of just the current file, in order to get the full path and file name for the current file you would need to change your code to something like this:
import os
list_raster = []
for (path, dirs, files) in os.walk(r"C:\Users\Douglas\Rasters\Testing folder"):
for file in files:
if "woody02.tif" in file:
# path will hold the current directory path where os.walk
# is currently looking and file would be the matching
# woody02.tif
list_raster.append(os.path.join(path, file))
# wait until all files are found before printing the list
print(list_raster)

Read all files in a folder and also the filenames in python? [duplicate]

This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
How to read all files and also the filenames?
I am using MAC so is there any there a different way to give path on MAC in Python?
Maybe something like this? Or os.listdir() is simpler if you don't need recursion.
Even on Windows, Python abstracts away the differences between operating systems if you use it well.
#!/usr/local/cpython-3.3/bin/python
import os
def main():
for root, dirs, files in os.walk('/home/dstromberg/src/outside-questions'):
for directory in dirs:
print('directory', os.path.join(root, directory))
for file_ in files:
print('file', os.path.join(root, file_))
main()
See http://docs.python.org/3/library/os.html for more info.

Retrieving the filenames from subdirectories [duplicate]

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.

Categories