2

I have a folder with different subfolders in it. I have to iterate through all the files and check for the occurrences of John and Jose and replace with Mikel and Mourinho respectively.

This is the script I have written in Python. It is working fine but when I encounter a .gif file it is giving me an error and it is not iterating further.

Can you please tell me why?

The error is

Traceback (most recent call last):
  File "C:\Users\sid\Desktop\script.py", line 33, in <module>
    os.chmod(path ,stat.S_IWRITE)
FileNotFoundError: [WinError 2] The system cannot find the file specified:'C:\Users\sid\Desktop\test\\images/ds_dataobject.gif.bak'

My code:

import os,stat
import fileinput
import sys

rootdir ='C:\Users\spemmara\Desktop\test'
searchTerms={"John":"Mikel", "Jose":"Mourinho"}

def replaceAll(file,searchExp,replaceExp):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp)
        sys.stdout.write(line)

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        path=subdir+'/'+file
        print(path)
        os.chmod(path ,stat.S_IWRITE)
        for key,value in searchTerms.items():
            replaceAll(path,key,value)
        os.chmod(path,stat.S_IREAD)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Wolf
  • 3,019
  • 3
  • 17
  • 14

2 Answers2

5

Use a raw string or Double blackslashes \\:

Without \\ or raw string '\t' is converted to a tab space:

>>> print 'C:\Users\spemmara\Desktop\test'
C:\Users\spemmara\Desktop   est

With raw string:

>>> print r'C:\Users\spemmara\Desktop\test'
C:\Users\spemmara\Desktop\test

Double blackslashes:

>>> print 'C:\\Users\\spemmara\\Desktop\\test'
C:\Users\spemmara\Desktop\test

Update:

'C:\Users\sid\Desktop\test\images/ds_dataobject.gif.bak'

Looking at the error you're trying to mix \ and / in a single path, better use os.path.join:

path = os.path.join(subdir, file)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • The code is working. It is replacing the files that have Jose with Mourinho but when there is a .gif file it is giving an error. So is it the problem of the raw string – Wolf Nov 04 '13 at 11:58
  • i have tried it with other folders also say , C:\Users\spemmara\Desktop\Source. Even for this folder the code is working. It is replacing, if it is the problem that u mentioned, then it should not give an error right – Wolf Nov 04 '13 at 12:00
  • The gifs is another problem unrelated with the rawstring – aIKid Nov 04 '13 at 12:00
  • 1
    @Wolf It works with `'Users\spemmara\Desktop\Source'` because this string doesn't contains any special characters like `\t`, `\a` etc – Ashwini Chaudhary Nov 04 '13 at 12:01
  • 1
    @Wolf Instead of using `path=subdir+'/'+file` use `path = os.path.join(subdir, file)`. – Ashwini Chaudhary Nov 04 '13 at 12:08
  • @hcwhsa thanks man os.path.join did the trick. But now there is another error. – Wolf Nov 04 '13 at 12:22
  • UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 545: character maps to – Wolf Nov 04 '13 at 12:22
  • @Wolf Please post that as a new question and [mark this one as solved](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). [UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to ](http://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character) – Ashwini Chaudhary Nov 04 '13 at 12:24
  • ya sure man thanks again. I would be much more grateful if u can solve that – Wolf Nov 04 '13 at 12:29
0

In addition to hcwhsa's answer, you can also work it the hard way, by using double backslashes:

rootdir = 'C:\\Users\\spemmara\\Desktop\\test'

I lost my crystal ball yesterday, but i guess another thing that is giving you problems, is because when your program founds a gif file, you're trying to write text to it.

If you don't want to do it, just test it with a condition:

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        path=subdir+'/'+file
        print(path)
        os.chmod(path ,stat.S_IWRITE)
        if '.gif' not in path:
            for key,value in searchTerms.items():
                replaceAll(path,key,value)
        os.chmod(path,stat.S_IREAD)
aIKid
  • 26,968
  • 4
  • 39
  • 65