简体   繁体   中英

os.rename giving an error

I am trying to have my code look for specific files, then rename them with my newName which I defined elsewhere. I am trying to debug this code, but I cannot think of what I am doing wrong when trying to implement the os.rename. I have see the SRC and DST as os.path.joins in other solutions I've found on stackoverflow. Can anyone provide any insight?

if os.path.exists(curr_dir + '\\Output\\Fab'):
        for (path, dirs, files) in os.walk(curr_dir + '\Output\Fab'):
            for f in files:
                name, ext = os.path.splitext(f)
                newName = replacement + ext
                os.rename(os.path.join(path, f), os.path.join(path, newName )

On windows, you get an error if the destination file allready exists.

Use '/' instead of '\\' so it doesn't escape the next char.

On the second line you use \\ instead of \\\\ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM