记录元数据时忽略(跳过)损坏的文件,然后继续

时间:2017-11-23 21:42:31

标签: python python-3.x

元数据记录代码卡在损坏的文件上,并显示如下所示的错误。

如何跳过(忽略)损坏的文件并继续程序?

代码(第68-87行和第198-204行):

#lines 68-87:
def createBasicInfoListFromDisk():

    global diskCompareListDetails, onlyFileNameOnDisk, driveLetter,walk_dir

    walk_dir = os.path.abspath(walk_dir)
    for root, subdirs, files in os.walk(walk_dir, topdown=True, onerror=None, followlinks=True ):
        for filename in files:
            file_path = os.path.join(root, filename)
            temp = file_path.split(":")
            driveLetter = temp[0]
            filePathWithoutDriveLetter = temp[1]
            fileSize = os.path.getsize(file_path)
            mod_on =  get_last_write_time(file_path)
            print('\t- file %s (full path: %s)' % (filename, file_path))
            print('FileName : {filename} is of size {size} and was modified on{mdt}'.format(filename=file_path,size=fileSize,mdt=mod_on ))

            diskCompareListDetails.append("\"" + filePathWithoutDriveLetter+"\",\""+str(fileSize) + "\",\"" + mod_on +'"')
            onlyFileNameOnDisk.append("\""+filePathWithoutDriveLetter+"\"")

    return


#lines 198-204:
    foundFile = 0
    foundFile=findAndReadCSVFile(csvfilewithPath)
    createBasicInfoListFromDisk()
    compareLogAndDiskLists()
    displayInfoForUserInput()
    processFiles(foundFile)
    writeCSVFile(csvfilewithPath)

错误:

FileName : T:\STBY\file1.txt is of size 1241 and was modified on2006-03-15 20:35:00
Traceback (most recent call last):
  File "c:\scripts\test.py", line 200, in <module>
    createBasicInfoListFromDisk()
  File "c:\scripts\test.py", line 79, in createBasicInfoListFromDisk
    fileSize = os.path.getsize(file_path)
  File "C:\Python\Python36\lib\genericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'T:\\STBY\\file1.txt'

修改 - 没有错误消息,但有50%的文件被跳过,文件,PowerShell脚本识别为ok:

def createBasicInfoListFromDisk():
    try:
        global diskCompareListDetails, onlyFileNameOnDisk, driveLetter,walk_dir

        walk_dir = os.path.abspath(walk_dir)
        for root, subdirs, files in os.walk(walk_dir, topdown=True, onerror=None, followlinks=True ):
            for filename in files:
                file_path = os.path.join(root, filename)
                temp = file_path.split(":")
                driveLetter = temp[0]
                filePathWithoutDriveLetter = temp[1]
                fileSize = os.path.getsize(file_path)
                mod_on =  get_last_write_time(file_path)
                print('\t- file %s (full path: %s)' % (filename, file_path))
                print('FileName : {filename} is of size {size} and was modified on{mdt}'.format(filename=file_path,size=fileSize,mdt=mod_on ))

                diskCompareListDetails.append("\"" + filePathWithoutDriveLetter+"\",\""+str(fileSize) + "\",\"" + mod_on +'"')
                onlyFileNameOnDisk.append("\""+filePathWithoutDriveLetter+"\"")

    except OSError:
        pass
    return "ERROR"

1 个答案:

答案 0 :(得分:1)

一种方法是使用try-except:

try:
    fileSize = os.path.getsize(file_path)
except OSError as e:
    fileSize = -1
    print('error thrown when handle {0}'.format(file_path)

或者,您可以在调用getsize()之前检查存在的文件:

fileSize = -1 if not os.path.exists(file_path) else os.path.getsize(file_path)