Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法指定Python在给定目录中循环的文件范围?_Python_File_File Conversion - Fatal编程技术网

有没有办法指定Python在给定目录中循环的文件范围?

有没有办法指定Python在给定目录中循环的文件范围?,python,file,file-conversion,Python,File,File Conversion,我有一个脚本,它迭代目录中的文件,将它们从一种格式转换为另一种格式。不幸的是,我没有考虑到与文件所在的网络驱动器失去连接的可能性,因此终止了我的脚本。在发生错误的情况下,为了跟踪脚本进入目录的距离,我确实让程序向我显示它读取的最后一个文件。我想从脚本停止的文件开始,而不是从头开始 下面是我的原稿。此脚本将DBF格式转换为CSV格式 import os from dbfread import DBF import pandas as pd directory = 'Directory conta

我有一个脚本,它迭代目录中的文件,将它们从一种格式转换为另一种格式。不幸的是,我没有考虑到与文件所在的网络驱动器失去连接的可能性,因此终止了我的脚本。在发生错误的情况下,为了跟踪脚本进入目录的距离,我确实让程序向我显示它读取的最后一个文件。我想从脚本停止的文件开始,而不是从头开始

下面是我的原稿。此脚本将DBF格式转换为CSV格式

import os
from dbfread import DBF
import pandas as pd

directory = 'Directory containing files'

for file in os.listdir(directory):

        if file.startswith('File_Prefix') and file.endswith('.DBF'):
            file_path = os.path.join(directory, file)
            print(f'\nReading in {file}...')
            dbf = DBF(file_path)
            dbf.encoding = 'utf-8'
            dbf.char_decode_errors = 'ignore'
            print('\nConverting to DataFrame...')
            df = pd.DataFrame(iter(dbf))
            df.columns.astype(str)
            print(df)
            print('\nWriting to CSV...')
            dest_directory = 'Destination_Directory\\%s.csv' % (File_Prefix + file.strip('.DBF'))
            df.to_csv(dest_directory, index = False)
            print(f'\nConverted {file} to CSV. Moving to next file...')

        elif file.startswith(Another_File_Prefix) and file.endswith('.DBF'):
            print('File not needed.')
            continue
        elif file.endswith('.FPT'):
            print('Skipping FPT file.')
            continue

        elif file.startswith('Another_file_prefix') and file.endswith('.DB~'):
            print('All files converted to CSV.')
            break

        else:
            print('\nFile not found or error.')
            print(f'Last file read in was {file}.')
我可以修改什么来指定读取的最后一个文件并从那里开始,同时忽略以前转换的文件?目录中的文件名相当模糊,只是一个字母和一个数字,随着您向下遍历目录而增加(例如“A0001.DBF”、“A0002.DBF”、“A0003.DBF”等)

我最初的解决方案是将最后一个文件分配给一个变量,然后修改我的“if”语句

start_file = last_file_read_in

for file in os.listdir(directory):
    if file == start_file:
       #run conversion code
       #continue iterating through each file starting from this point

您可以在目标目录中查看已转换的文件,并插入一个条件,以仅在转换的文件不存在时进行转换。@snnguyen感谢您的回复。我忘记了
os.path.exists(path)
。从逻辑上讲,我如何写出来,以便脚本首先检查目标目录以查看文件是否已转换,如果未转换,则从原始目录读入未转换的文件?由于文件名未重命名,且仅因文件结尾不同,将目标目录中的所有文件名放入一个集合中。如果文件名不在集合中,请将其转换,否则将跳过。