简体   繁体   English

Python文件IO和zipfile。 尝试遍历文件夹中的所有文件,然后使用Python遍历各个文件中的文本

[英]Python file-IO and zipfile. Trying to loop through all the files in a folder and then loop through the texts in respective file using Python

Trying to extract all the zip files and giving the same name to the folder where all the files are gonna be. 尝试提取所有zip文件,并为所有文件所在的文件夹指定相同的名称。 Looping through all the files in the folder and then looping through the lines within those files to write on a different text file. 遍历文件夹中的所有文件,然后遍历那些文件中的行以在不同的文本文件上写入。
This is my code so far: 到目前为止,这是我的代码:

#!usr/bin/env python3
import glob
import os
import zipfile

zip_files = glob.glob('*.zip')
for zip_filename in zip_files:
    dir_name = os.path.splitext(zip_filename)[0]
    os.mkdir(dir_name)
    zip_handler = zipfile.ZipFile(zip_filename, "r")
    zip_handler.extractall(dir_name)

path = dir_name
fOut = open("Output.txt", "w")

for filename in os.listdir(path):
    for line in filename.read().splitlines():
        print(line)
        fOut.write(line + "\n")
fOut.close()

This is the error that I encounter: 这是我遇到的错误:

for line in filename.read().splitlines():
AttributeError: 'str' object has no attribute 'read'

You need to open the file and also join the path to the file, also using splitlines and then adding a newline to each line is a bit redundant: 您需要打开文件,还使用分割线将文件连接到文件的路径,然后在每行中添加换行符有点多余:

path = dir_name
with open("Output.txt", "w") as fOut:    
    for filename in os.listdir(path):
        # join filename to path to avoid file not being found
        with open(os.path.join(path, filename)):
            for line in filename:
                fOut.write(line)

You should always use with to open your files as it will close them automatically. 您应始终使用with来打开文件,因为它将自动关闭文件。 If the files are not large you can simply fOut.write(f.read()) and remove the loop. 如果文件不大,则可以简单地fOut.write(f.read())并删除循环。

You also set path = dir_name which means path will be set to whatever the last value of dir_name was in your first loop which may or may not be what you want. 您还设置了path = dir_name ,这意味着path将被设置为您的第一个循环中dir_name的最后一个值,无论它是否是您想要的。 You can also use iglob to avoid creating a full list zip_files = glob.iglob('*.zip') . 您也可以使用iglob来避免创建完整列表zip_files = glob.iglob('*.zip')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM