Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 动态读取文件并将其加载到Python中_Python 3.x - Fatal编程技术网

Python 3.x 动态读取文件并将其加载到Python中

Python 3.x 动态读取文件并将其加载到Python中,python-3.x,Python 3.x,在Python中,是否有一种方法可以动态导入csv或文本文件。我们每周处理多个具有不同名称的文件,我不想在每次脚本运行时手动更新with open语句。我有一个函数来读取文件名,并将其传递给变量,以供以后在代码中使用 我可以查看和读取目录中的文件,但我不确定是否可以将文件夹的内容添加到变量中,然后在with open语句中使用该变量 import os os.chdir('T:\Credit Suite') DIR = os.listdir() print(DIR) import csv,sy

在Python中,是否有一种方法可以动态导入csv或文本文件。我们每周处理多个具有不同名称的文件,我不想在每次脚本运行时手动更新with open语句。我有一个函数来读取文件名,并将其传递给变量,以供以后在代码中使用

我可以查看和读取目录中的文件,但我不确定是否可以将文件夹的内容添加到变量中,然后在with open语句中使用该变量

import os
os.chdir('T:\Credit Suite')
DIR = os.listdir()
print(DIR)

import csv,sys
with open('July 19.csv',mode='r') as csv_file:
    ROWCOUNT = 0
    FILENAME = (csv_file.name)
    output = csv.writer(open('test2.txt', 'w', newline=''))
    reader =csv.DictReader(csv_file)
    for records in reader:
        ROWCOUNT += 1
        EIN = records['EIN']
        DATE = records['Date Established']
        DUNS = records['DUNS #']
        COMPANYNAME = records['Company Name']
        lineout =('<S>'+ EIN+'$EIN '+EIN+'*'+DATE+')'+ COMPANYNAME +'#D-U-N-S '+DUNS).upper()
        output.writerow([lineout])
        print("writing completed")
导入操作系统
chdir('T:\Credit Suite')
DIR=os.listdir()
打印(目录)
导入csv,系统
打开('July 19.csv',mode='r')作为csv\u文件:
行计数=0
文件名=(csv_file.name)
output=csv.writer(打开('test2.txt','w',换行符='')
reader=csv.DictReader(csv\u文件)
对于读卡器中的记录:
行计数+=1
EIN=记录['EIN']
日期=记录[“建立日期”]
DUNS=记录['DUNS#']
COMPANYNAME=记录['COMPANYNAME']
lineout=(''+EIN+'$EIN'+EIN+'*'+DATE+')'+COMPANYNAME+'#D-U-N-S'+DUNS)。上()
output.writerow([lineout])
打印(“书写完成”)
我将运行我的脚本时,一个文件击中一个文件夹使用监视器和调度程序在一个自动化的过程。无论入站文件名在文件夹中标记为什么,我都希望代码运行,并且我不必每次手动更新文件名的代码或将文件名更改为标准名称。

返回目录中所有文件的列表,您只需循环所有文件即可:

import os
os.chdir('T:\Credit Suite')
DIR = os.listdir()
print(DIR)

import csv,sys
for file in DIR:
    if file.endswith('.csv'):
        with open(file,mode='r') as csv_file:
            ROWCOUNT = 0
            FILENAME = (csv_file.name)
            output = csv.writer(open(FILENAME + '_output.txt', 'w', newline=''))
            reader =csv.DictReader(csv_file)
            all_lines = []
            for records in reader:
                ROWCOUNT += 1
                EIN = records['EIN']
                DATE = records['Date Established']
                DUNS = records['DUNS #']
                COMPANYNAME = records['Company Name']
                lineout =('<S>'+ EIN+'$EIN '+EIN+'*'+DATE+')'+ COMPANYNAME +'#D-U-N-S '+DUNS).upper()
                all_lines.append(lineout)
            output.writerow(all_lines)
            print("writing completed")
        # remove file to avoid reprocessing the file again in the next run
        # of the script, or just move it elsewhere with os.rename
        os.remove(file)
导入操作系统
chdir('T:\Credit Suite')
DIR=os.listdir()
打印(目录)
导入csv,系统
对于目录中的文件:
如果文件.endswith('.csv'):
打开(文件,模式为r')作为csv\u文件:
行计数=0
文件名=(csv_file.name)
output=csv.writer(打开(文件名+'_output.txt',w',换行符=“”))
reader=csv.DictReader(csv\u文件)
所有_行=[]
对于读卡器中的记录:
行计数+=1
EIN=记录['EIN']
日期=记录[“建立日期”]
DUNS=记录['DUNS#']
COMPANYNAME=记录['COMPANYNAME']
lineout=(''+EIN+'$EIN'+EIN+'*'+DATE+')'+COMPANYNAME+'#D-U-N-S'+DUNS)。上()
所有_行。追加(行输出)
output.writerow(所有_行)
打印(“书写完成”)
#删除文件以避免下次运行时再次重新处理该文件
#或者使用os.rename将其移动到其他位置
删除(文件)
返回目录中所有文件的列表,您可以循环所有文件:

import os
os.chdir('T:\Credit Suite')
DIR = os.listdir()
print(DIR)

import csv,sys
for file in DIR:
    if file.endswith('.csv'):
        with open(file,mode='r') as csv_file:
            ROWCOUNT = 0
            FILENAME = (csv_file.name)
            output = csv.writer(open(FILENAME + '_output.txt', 'w', newline=''))
            reader =csv.DictReader(csv_file)
            all_lines = []
            for records in reader:
                ROWCOUNT += 1
                EIN = records['EIN']
                DATE = records['Date Established']
                DUNS = records['DUNS #']
                COMPANYNAME = records['Company Name']
                lineout =('<S>'+ EIN+'$EIN '+EIN+'*'+DATE+')'+ COMPANYNAME +'#D-U-N-S '+DUNS).upper()
                all_lines.append(lineout)
            output.writerow(all_lines)
            print("writing completed")
        # remove file to avoid reprocessing the file again in the next run
        # of the script, or just move it elsewhere with os.rename
        os.remove(file)
导入操作系统
chdir('T:\Credit Suite')
DIR=os.listdir()
打印(目录)
导入csv,系统
对于目录中的文件:
如果文件.endswith('.csv'):
打开(文件,模式为r')作为csv\u文件:
行计数=0
文件名=(csv_file.name)
output=csv.writer(打开(文件名+'_output.txt',w',换行符=“”))
reader=csv.DictReader(csv\u文件)
所有_行=[]
对于读卡器中的记录:
行计数+=1
EIN=记录['EIN']
日期=记录[“建立日期”]
DUNS=记录['DUNS#']
COMPANYNAME=记录['COMPANYNAME']
lineout=(''+EIN+'$EIN'+EIN+'*'+DATE+')'+COMPANYNAME+'#D-U-N-S'+DUNS)。上()
所有_行。追加(行输出)
output.writerow(所有_行)
打印(“书写完成”)
#删除文件以避免下次运行时再次重新处理该文件
#或者使用os.rename将其移动到其他位置
删除(文件)

谢谢。我一发布这个问题,就发现了os.walk方法。我在下面发布了我目前拥有的代码及其工作原理。不确定这是否是最好的方法。我也会尝试一下您的代码。请记住,os.walk将搜索文件夹的所有层次结构,并将其与仅列出当前目录内容的os.listdir相匹配。此外,在代码中,您将只处理os.walk将找到的最后一个csv文件,因为它重新分配了变量f。我不确定这是不是你想要的。我还编辑了答案以包含变量输出文件名,这样它将为您处理的每个csvThank生成输出文件。我一发布这个问题,就发现了os.walk方法。我在下面发布了我目前拥有的代码及其工作原理。不确定这是否是最好的方法。我也会尝试一下您的代码。请记住,os.walk将搜索文件夹的所有层次结构,并将其与仅列出当前目录内容的os.listdir相匹配。此外,在代码中,您将只处理os.walk将找到的最后一个csv文件,因为它重新分配了变量f。我不确定这是不是你想要的。我还编辑了答案以包含变量输出文件名,这样它将为每个已处理的csv生成输出文件