Search code examples
pythonpython-2.7file-renamewindowserror

Rename txt file. Edited version: [Error 183] Cannot create a file when that file already exists


I'm working with python 2.7x. I'm quite new to python and will really appreciate your help. I have read many posts including those shown below to name a few about this error:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat' --> my file close instruction is at the end already.

python 2 [Error 32] The process cannot access the file because it is being used by another process --> I can't use shutil as there's some error in the python program I'm using and I cant edit the pathway as my computer is adminstrative protected.

Rename Files in Python -->After following the suggestion, I got NameError: name 'rename' is not defined instead... :/

etc etc.

After trying to rectify my codes, I'm still getting the same error.

What I would like to do is: I read the files in a directory, if any of the files contains a specific string, I would rename the text file (i.e. first.txt to firstfound.txt.)

EDITED VERSION: I tried moving the abc.close() before I rename the file:

import os

fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
 if not line.find("scramble") : 
  continue
 oldfile = fname
abc.close() 
if oldfile == fname:
 for title in fname:
    endname = title.find('.txt')
    oldtitle = title[:endname]
    newfile = oldtitle +"found.txt"
    os.rename(oldfile,newfile)

But I have this error instead for the last line. os.rename(oldfile,newfile) WindowsError: [Error 183] Cannot create a file when that file already exists. There's no file with the new name in my folder. Thanks so much for your advice!

EDITED VERSION 2: I have also tried this other set of codes but it gave me WindowsError: [Error 5] Access is denied. May I know if there's such a thing that I cannot rename the txt file because I have no administrator privilege? Thank you!

import os

fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
 if not line.find("scramble") : 
  continue
 oldfile = fname
abc.close() 

if (oldfile == fname): #+'/' +  "a.txt"
 for title in fname:
    endname = title.find('.txt')
    oldtitle = title[:endname]
    new_file = oldtitle +'/' +  "a.txt"
    os.rename(fname,new_file)

INITIAL version: The error I got is at the os.rename line. "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"

My whole program code is as shown below:

import os

fname = raw_input("Enter file name: ")
#fill in file directory here
abc = open(fname)
for line in abc:
 if not line.find("scramble") : 
  continue
 old_file = fname
 for title in fname:
    endname = title.find('.txt')
    oldtitle = title[:endname]
    new_file = oldtitle +'/' +  "found.txt"
    os.rename(old_file,new_file) ##WindowsError: [Error 32] The process cannot access the file because it is being used by another process
abc.close() 

I don't understand why this error persists. (I have closed all the files & folders). Thank you very much!


Solution

  • The problem is most likely due to your open() call in your code. The open() function in python opens file file for reading/writing, so if you open a file then you cannot call rename on it as it is open in another location.

    Instead you should call

    abc.close() 
    

    before renaming your file.

    See this link for more information regarding file I/O.