Renaming multiple files in a directory using Python

半腔热情 提交于 2019-12-17 06:38:30

问题


I'm trying to rename multiple files in a directory using this Python script:

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

for file in files:
    os.rename(file, str(i)+'.jpg')
    i = i+1

When I run this script, I get the following error:

Traceback (most recent call last):
  File "rename.py", line 7, in <module>
    os.rename(file, str(i)+'.jpg')
OSError: [Errno 2] No such file or directory

Why is that? How can I solve this issue?

Thanks.


回答1:


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.




回答2:


You have to make this path as a current working directory first. simple enough. rest of the code has no errors.

to make it current working directory:

os.chdir(path)



回答3:


As per @daniel's comment, os.listdir() returns just the filenames and not the full path of the file. Use os.path.join(path, file) to get the full path and rename that.

import os
path = 'C:\\Users\\Admin\\Desktop\\Jayesh'
files = os.listdir(path)
for file in files:
   os.rename(os.path.join(path, file), os.path.join(path, 'xyz_' + file + '.csv'))



回答4:


If your files are renaming in random manner then you have to sort the files in the directory first. The given code first sort then rename the files.

import os
import re
path = 'target_folder_directory'
files = os.listdir(path)
files.sort(key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])
for i, file in enumerate(files):
    os.rename(path + file, path + "{}".format(i)+".jpg")



回答5:


Just playing with the accepted answer define the path variable and list:

path = "/Your/path/to/folder/"
files = os.listdir(path)

and then loop over that list:

for index, file in enumerate(files):
    #print (file)
    os.rename(path+file, path +'file_' + str(index)+ '.jpg')

or loop over same way with one line as python list comprehension :

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

I think the first is more readable, in the second the first part of the loop is just the second part of the list comprehension




回答6:


I worked on a quick and flexible script to take care of displaying differences, asking for confirmations, and renaming. If you need a working solution, you can copy this script and place it in the folder of which you want to rename files. https://gist.github.com/aljgom/81e8e4ca9584b481523271b8725448b8

It renames files in current directory by passing "rename functions", each function can take care of a change. Then determines the changes that each function will make and displays the differences using colors, and asks for confirmation to perform the changes. Works on pycharm, haven't tested it in different consoles



来源:https://stackoverflow.com/questions/37467561/renaming-multiple-files-in-a-directory-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!