Rename Multiple Files in a Directory in Python

Renaming multiple files in a directory using Python

You are not giving the whole path while renaming, do it like this:

import os
path = '/Users/myName/Desktop/directory'
files = os.listdir(path)

for index, file in enumerate(files):
os.rename(os.path.join(path, file), os.path.join(path, ''.join([str(index), '.jpg'])))

Edit: Thanks to tavo, The first solution would move the file to the current directory, fixed that.

Rename multiple files in a directory in Python

Use os.rename(src, dst) to rename or move a file or a directory.

$ ls
cheese_cheese_type.bar cheese_cheese_type.foo
$ python
>>> import os
>>> for filename in os.listdir("."):
... if filename.startswith("cheese_"):
... os.rename(filename, filename[7:])
...
>>>
$ ls
cheese_type.bar cheese_type.foo

Rename Multiple folder in a directory with python

The src and dst aren't the absolute path, so it's trying to rename a file from the directory of the python script.

You should be able to fix this just by replacing os.rename(src, dst) with os.rename(os.path.join(path, src), os.path.join(path, dst)) to specify absolute path.

Rename multiple files in multiple sub folders using python

You need to use the file in the dir where it is.

import os

# assign directory
directory = "all Student folders"

# iterate over files

for root, _, files in os.walk(directory):
for file_name in files:
if file_name == "Work.pdf":
os.rename(f"{root}/{file_name}", f"{root}/homework1.pdf")
else:
new_name = file_name.replace(f"Work", "homework")
os.rename(f"{root}/{file_name}", f"{root}/{new_name}")

Rename multiple files inside multiple folders

The pathlib module, which was new in Python 3.4, is often overlooked. I find that it often makes code simpler than it would otherwise be with os.walk.

In this case, .glob('**/*.*') looks recursively through all of the folders and subfolders that I created in a sample folder called example. The *.* part means that it considers all files.

I put path.parts in the loop to show you that pathlib arranges to parse pathnames for you.

I check that the string constant '34562346' is in its correct position in each filename first. If it is then I simply replace it with the items from .parts that is the next level of folder 'up' the folders tree.

Then I can replace the rightmost element of .parts with the newly altered filename to create the new pathname and then do the rename. In each case I display the new pathname, if it was appropriate to create one.

>>> from pathlib import Path
>>> from os import rename
>>> for path in Path('example').glob('**/*.*'):
... path.parts
... if path.parts[-1][3:11]=='34562346':
... new_name = path.parts[-1].replace('34562346', path.parts[-2])
... new_path = '/'.join(list(path.parts[:-1])+[new_name])
... new_path
... ## rename(str(path), new_path)
... else:
... 'no change'
...
('example', 'folder_1', 'id.34562346.6.a.txt')
'example/folder_1/id.folder_1.6.a.txt'
('example', 'folder_1', 'id.34562346.wax.txt')
'example/folder_1/id.folder_1.wax.txt'
('example', 'folder_2', 'subfolder_1', 'ty.34562346.90.py')
'example/folder_2/subfolder_1/ty.subfolder_1.90.py'
('example', 'folder_2', 'subfolder_1', 'tz.34562346.98.py')
'example/folder_2/subfolder_1/tz.subfolder_1.98.py'
('example', 'folder_2', 'subfolder_2', 'doc.34.34562346.implication.rtf')
'no change'

How to replace characters and rename multiple files?

This should get it done:

import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", "").replace("_ ", ""))


Related Topics



Leave a reply



Submit