文件存在但未找到(Python)

时间:2017-06-08 19:15:38

标签: python

有一个网络驱动器,其中包含一堆存储为“部件号”.pdf文件的图纸。我正在尝试将每个文件复制到一个新目录并将它们重命名为'reference ID'.pdf。当我运行这个方法时,它会在每个文件上记录“找不到文件”(文件夹中有超过7000个),即使它们在那里。我猜这与路径错误有关,但我对python很新,所以我似乎无法追踪它。如果有人可以看看我的代码我会非常感激!我正在使用python 3.6,如果这有任何区别。

def convert_files(self):

    # Directory that holds the drawings files
    drawings_file_path = '\\\\BILBO\\Engineering\\drawings\\approved drawings'

    # Open the file that associates part numbers with reference IDs
    with open('part_reference_list.csv', newline='') as csvfile:
        line_reader = csv.reader(csvfile, delimiter=',')
        for row in line_reader:

            # Read the part number and reference ID from the reference list
            logging.debug("Reading row {} from part reference list...".format(row))

            # Store the part number and reference ID
            # Truncates unused characters from the part number
            part_number = row[0][3:11]
            ref_ID = row[1]

            try:
                logging.debug("Searching for {}...".format(part_number))

                # Set the source and destination paths
                # and copy the .pdf to the new diretory
                dest_file_name = '{}\\{}.pdf'.format(self.file_path, ref_ID)
                src_file_name = '{}\\{}.pdf'.format(drawings_file_path, part_number)
                copyfile(src_file_name, dest_file_name)

                logging.info("Copied {}.pdf to {}.pdf".format(part_number, ref_ID))

            except FileNotFoundError:
                logging.warning("Drawing file for part: {} was not found.".format(part_number))

0 个答案:

没有答案