Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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/1/dart/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 - Fatal编程技术网

Python读写文件

Python读写文件,python,Python,我有1000多个文件,我正试图连接成一个大文件。它们都是.txt文件,并且都包含相同的数据格式 我似乎能够使用PyCharm读取每个文件中的每一行,并将其写入输出文件。我数了数行数,结果是大约393225行,大致准确(我没有手动计算)。然而,当我计算输出文件中的行数时,它只有401行,我不知道为什么它没有全部完成 以下是我在Python中使用的当前代码: import datetime from pathlib import Path start_date = datetime.date(ye

我有1000多个文件,我正试图连接成一个大文件。它们都是.txt文件,并且都包含相同的数据格式

我似乎能够使用PyCharm读取每个文件中的每一行,并将其写入输出文件。我数了数行数,结果是大约393225行,大致准确(我没有手动计算)。然而,当我计算输出文件中的行数时,它只有401行,我不知道为什么它没有全部完成

以下是我在Python中使用的当前代码:

import datetime
from pathlib import Path

start_date = datetime.date(year=2019, month=3, day=22)
end_date = datetime.date(year=2015, month=12, day=1)

list = []

if start_date == end_date:
    for n in range((end_date - start_date).days + 1):
        list.append(start_date + datetime.timedelta(n))
else:
    for n in range((start_date - end_date).days + 1):
        list.append(start_date - datetime.timedelta(n))

countFile = 0
countLines = 0

for d in reversed(list):
    date = str(d)
    path = '/Users/stephankokkas/notuploading/TESTFILES/PRICEDATA/' + date + '/Race.txt'

    raceFile = Path(path)
    if raceFile.is_file():

        with open('/Users/stephankokkas/notuploading/TESTFILES/finalRaceFile/FinalRace.txt', 'w') as outfile:
            with open(path) as infile:
                for line in infile:
                    outfile.write(line)
                    countLines = countLines + 1
                    print(line)
    else:
        print("file NOT FOUND")



print(countLines)

countLines = 0
with open('/Users/stephankokkas/notuploading/TESTFILES/finalRaceFile/FinalRace.txt', 'r') as infile:
    for line in infile:
        countLines = countLines + 1
print(countLines)

这是输出

393225
401
我不知道为什么他们不是同一个数字。。。。我希望他们是

打开输出文件时,数据范围仅为2019-03-22至2019-03-22

它似乎只是在做它找到的最后一个文件


很可能是一些显而易见的事情,但一些帮助会很好。谢谢

当您使用
'w'
模式打开文件时,您将覆盖它。您需要以
'a'
模式打开才能附加

e、 g


参见

Tl的可能副本;dr,您使用了错误的写入模式来写入
FinalRace.txt
请使用
a
而不是
w
作为建议:不要将列表用作变量名-python允许您这样做,但您正在将python类重新指定为变量
with open('fielname.txt', 'a') as outfile: