Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing carriage return characters from a file using python

import os
import sys

files = os.listdir(sys.argv[1])
for file in files:
    if file[-4:] == ".png":
        os.rename(file, file.replace('\r', ''))

Am using the above code to remove \r from the file name, but some how when I execute I get the following error

Traceback (most recent call last):
  File "renameImages.py", line 9, in <module>
    os.rename(f, f.replace('\r', ''))
OSError: [Errno 2] No such file or directory

Where am I going wrong?

like image 439
LearnCode Avatar asked Apr 02 '11 00:04

LearnCode


1 Answers

You didn't tell it the directory of the file, that was declared in argv[1] try os.rename(sys.argv[1]+"/file",sys.argv[1]+"/"+replace('\r'','') (or '\\' for Windows).

like image 151
Property404 Avatar answered Sep 24 '22 04:09

Property404