2

I have files in a folder. Their names look like:

hello_1
music
hello_2
music_2015

I would like to rename them sequentially:

1
2
3
4

My python script is:

import glob, os

n = 1
for filename in glob.glob('data\*'):
    os.rename(filename, str(n))
    n += 1

I am getting the following error:

[Error 183] Cannot create a file when that file already exists
kevin
  • 1,914
  • 4
  • 25
  • 30
  • 1
    Is there a file named "1" in that folder already? – yuvi Dec 30 '15 at 18:49
  • No, but some names include the number "1" such as pm_11_1.txt – kevin Dec 30 '15 at 18:50
  • I'll reiterate - is there *any* file that is just a number (like "1" or "5" or what have you)? Because the error clearly says that you're trying to rename a file into a name of an already existing file. – yuvi Dec 30 '15 at 18:52
  • no he is in the parent folder ,... he probably ran it part way and has already renamed some of the files to numbers – Joran Beasley Dec 30 '15 at 18:53
  • how will you decide which name to give to which file? Is there any specific order we need to rename them? – python Dec 30 '15 at 18:55
  • Possible duplicate of [file renaming using python](http://stackoverflow.com/questions/29075818/file-renaming-using-python) – PyNEwbie Dec 30 '15 at 19:45
  • Possible duplicate of [Batch Renaming of Files in a Directory](http://stackoverflow.com/questions/225735/batch-renaming-of-files-in-a-directory) – jezrael Dec 31 '15 at 12:00

4 Answers4

4

Try this version:

import os
data = os.path.abspath("data/")
for i, f in enumerate(os.listdir(data)):
    src = os.path.join(data, f)
    dst = os.path.join(data, (str(i + 1)))
    os.rename(src, dst)

It's using normalized absolute paths. It works for me. It's kind of more concise and requires only os library instead of two.

vrs
  • 1,922
  • 16
  • 23
2

That script rename that files and change their location to the parent folder, in this case, parent of the data folder. Maybe you have some file with these number in the parent folder. You should attach the folder name to the rename function's second parameter.

Mehran Meidani
  • 341
  • 1
  • 13
2

os.rename(src, dst)

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.

Your destination is wrong. It is your current directory + n while it should be the data directory + n.

Community
  • 1
  • 1
khajvah
  • 4,889
  • 9
  • 41
  • 63
0

You can try this script rename_files.py. you should place this script inside the folder where all your files are located.

Also note:

os.rename(src, dst)

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.

import os

list_of_files = os.listdir(os.getcwd())

n = 1
for filename in list_of_files:
    if not filename.endswith(".py"):
        os.rename(filename, str(n))
        n += 1

Before running this script you can see the file_names and directory. enter image description here

After running this script. I hope this is helpful enter image description here

python
  • 4,403
  • 13
  • 56
  • 103