【问题标题】:change the first word multiple text files at once using python使用python一次更改多个文本文件的第一个单词
【发布时间】:2020-08-19 00:08:25
【问题描述】:

我有很多文本文件

我想改变他们的第一个词我试过这个,但没有奏效。 我想阅读所有文本文件,打开它们并将第一个单词(8 个字符)更改为一个相同的短语

import os

for filename in os.listdir("data"):
    if filename.endswith(".txt"):
        content = filename.read()
        with open(os.path.join("data" , filename) , "w") as outfile:
            outfile.write("new word" + content[8:])  

错误:

Traceback (most recent call last): File "change.py", line 5, in <module> content = filename.read() AttributeError: 'str' object has no attribute 'read'

【问题讨论】:

  • 有问题吗?请参阅How to Ask、help center。
  • 这里目前没有问题。我猜这会导致异常。例外是告诉你出了什么问题。它说的应该是可以理解的,但如果不是,那么你可以问你不明白的地方。

标签: python file-handling


【解决方案1】:

根据我对您问题的理解,我想出了这个解决方案。如果这不是您寻求的解决方案,请告诉我。

import os



def fread(file):
    with open(file, "r") as f:
        lines = f.readlines()
        f.close()
        return lines



def replace(file,folder):
    lines = fread(file)
    with open(file,'w') as four:
        
        for x in lines:
            print(x[0])
            s = x[0] # first word with n characters
    
            fout.write(x.replace(s,'your desired replacement'))
        fout.close()

    

if __name__ == "__main__":
    folder = "your folder containing files"
    curretndir = os.getcwd()
    os.chdir(folder)
    for files in os.listdir(folder):
        if files.endswith(".txt"):
            replace(files, folder)

如果我知道文件的内容就好了。根据需要,您必须使用 .split() 函数。我假设您的内容采用以下形式: 字 1 字 2 字 3 ...\n wordn wordn+1 .... \n

【讨论】:

    【解决方案2】:

    我已经标记了你的错误。

    import os
    
    for filename in os.listdir("data"):
        if filename.endswith(".txt"):
            with open(os.path.join("data" , filename),"r") as f: #<<-- you forgot to open the file like this
                content = f.read()
            with open(os.path.join("data" , filename) , "r") as outfile: #<<-- you are trying to read the file by using "r" change this to "w"
                outfile.write("new word" + content[8:]) #<<-- you are writing to the file
    

    【讨论】:

    • 同样的问题:回溯(最近一次调用最后一次):文件“change.py”,第 5 行,在 content = filename.read() AttributeError: 'str' object has no attribute'阅读'
    • 您得到的错误是试图告诉您您正在尝试在 str 对象上应用 read 方法。仔细看...filename 只是字符串...
    • 当我使用你的代码时,我得到了 Traceback(最近一次调用最后一次):文件“change.py”,第 5 行,在 中,open(filename,"r") as f:#
    • 我将 open(filename,"r") as f: 更改为 with open(os.path.join("data" , filename) , "r") as f:
    【解决方案3】:
    import os
    
    for filename in os.listdir("data"):
        if filename.endswith(".txt"):
            with open(os.path.join("data" , filename) , "r") as f:
                content = f.read()
            with open(os.path.join("data" , filename) , "w") as outfile: 
                outfile.write("new word" + content[8:]) 
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2021-09-08
      • 2012-07-05
      相关资源
      最近更新 更多