4

Below codes is used for tidy up current folder.

import os, glob

print "Tidy up ScreenShot ..."
pwd = os.getcwd()

for file in glob.glob("*.png"):
    #print file.encode('utf-8')
    file_time = file.split(" ")
    #print file_time[0]
    file_times = file_time[0].split("-")
    file_time_year = file_times[0]
    file_time_month = file_times[1]
    if (file_time_year.isdigit() and file_time_month.isdigit()):
        #print file_time_year
        #print file_time_month
        target_dir = os.path.join(cwd, 'OLD', file_time_year, file_time_month)
        if (not(os.path.exists(target_dir))):
            os.makedirs(target_dir)
        try:
            os.rename(file, os.path.join(target_dir, file))
        except WindowsError:
            print "Error: %s" %file

Normally it runs OK, but if there are some special charters in the file name, like Chinese, it will fail with below error message:

Tidy up ScreenShot ...
Error: 2017-11-15 09_13_45-Download Windows 10 and 1 more page ?- Microsoft Edge.png
Error: 2017-11-17 14_14_48-IMG_20171117_141100.jpg ?- Photos.png
Error: 2017-11-17 14_36_18-IMG_20171117_143016.jpg ?- Photos.png
Error: 2017-11-17 16_18_17-IMG_20171117_161538.jpg ?- Photos.png
Error: 2017-12-05 17_40_29-Skype? .png
Error: 2017-12-06 12_37_37-No Permission - Inside and 1 more page ?- Microsoft Edge.png
Error: 2017-12-07 09_56_21-???Q16??_?????-?????_?????????_?????_????_?????.png
Error: 2017-12-07 09_58_23-???Q16??_?????-?????_?????????_?????_????_?????.png
Error: 2018-01-03 14_02_57-????????????????220v????????????-mall.com??.png
Error: 2018-01-03 14_03_39-????????????????220v????????????-mall.com??.png
Error: 2018-01-03 14_19_08-??R30??????????????????????????-mall.com??.png

How to fix this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Fisher
  • 376
  • 3
  • 14
  • You need to search a encoding which is matching with your kind of letters. So normal english/german/... "normal special chars" force UTF-8 encoding and transformation on terminal - or if you want everything you need to set it up for unicode. There are tonns of solutions on google so have fun. – LenglBoy Mar 20 '18 at 15:12
  • Try to cast the filename to unicode: `file = file.decode('utf-8')` – Błażej Michalik Mar 20 '18 at 15:12
  • I suggest changing `except WindowsError` so that you can actually see the exception that is being raised, maybe don't even catch it so that the entire application crashes. Only a filename doesn't give much information. – Rick Rongen Mar 20 '18 at 15:14
  • @BłażejMichalik Now it reports: Traceback (most recent call last): File "tidy.py", line 12, in file = file.decode('utf-8') File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0x99 in position 25: invalid start byte – Fisher Mar 20 '18 at 15:22
  • @RickRongen: After remove except: Traceback (most recent call last): File "tidy.py", line 25, in os.rename(file, os.path.join(target_dir, file)) WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect – Fisher Mar 20 '18 at 15:24
  • @Fisher Is the name showing up correctly in the file explorer? – Błażej Michalik Mar 20 '18 at 15:38

0 Answers0