Rename files

Task description

  • In a file folder, I have lots of files whose name are meaningless code, so I need to rename them.

  • If the filename includes no chinese in it, then convert it to number, ie, 1,2,3...

  • If the filename includes chinese, then don't change the name.

    Anatomy of task

  • Locate the file folder I want to make changes.

  • Read filenames->judge it includes chinese or not->if not, then rename the file

Difficulties

  • I really should learn about 're'. Typical error on Windows because the default user directory is C:\user\, so when you want to use this path as an string parameter into a Python function, you get a Unicode error, just because the \u is a Unicode escape. Any character not numeric after this produces an error.

  • os.rename(src,dst) should be abusolute path file not relative, so add :

    os.rename(os.path.join(directory,filename),os.path.join(directory,str(i)+'.jpg'))

Code

Github code link

Last updated