【问题标题】:Python: how to transform a string which carries the filename into a readable filename?Python:如何将带有文件名的字符串转换为可读的文件名?
【发布时间】:2016-02-10 01:17:49
【问题描述】:

我想将文件循环到一个目录中,在这些文件上做一些事情,然后为每个文件写出结果。 但是我的文件无法读取,因为 python 将文件名解释为字符串对象而不是可读文件。 有没有办法避免这种情况?

import re
import os


def create_filename_for_fileout (f1):
        fileout_n = f1.replace("TT", "out")
        fileout = "C:\\Users\\KP\\Desktop\\FSC_Treetag\\out\\"+str(fileout_n)
        return fileout

for file_in in os.listdir('C:\\Users\\KP\\Desktop\\FSC_Treetag'):
    filename = str(file_in)
    file_out = create_filename_for_fileout(filename)

    open(file_in, 'r')
    open(file_out, 'w')


    content_file = file_in.readlines()
    for ln in content_file:
        regex = re.compile('(.*\t(ADJ|ADV|NOM|VER:cond|VER:futu|VER:impe|VER:impf|VER:infi|VER:pper|VER:pres|VER:pres|VER:simp|VER:subi|VER:subp)\t(.*))')
        res = regex.search(ln)
        if res:
         # categ = res.group(2)
           lemme = res.group(3)
           file_out.write(str(lemme)+"\n")

    file_out.close()    
    file_in.close()

结果:

 content_file = file_in.readlines()
AttributeError: 'str' object has no attribute 'readlines'
>>> 

【问题讨论】:

  • “python 将文件名解释为字符串对象而不是可读文件”是什么意思?
  • @DavidZ content_file = file_in.readlines() AttributeError: 'str' object has no attribute 'readlines' >>>

标签: python loops file-handling


【解决方案1】:

您没有将 open 分配给任何要使用的变量。

# Change
open(file_in, 'r')
open(file_out, 'w')
# to
input_file = open(file_in, 'r')
output_file = open(file_out, 'w')

for ln in input_file:
    # do your processing
    if res:
        lemme = res.group(3)
        output_file.write(str(lemme) + "\n")

【讨论】:

  • 它有效!为什么在这种情况下需要分配一个变量?
  • 您的变量file_infile_out 只是文件名的字符串,而不是文件处理程序本身。因此,当您打开文件时,它会返回一个 file 对象,并且不会将文件名变量更改为 file 对象。这就是为什么您需要将 open 函数返回的文件对象分配给您将使用的变量。
【解决方案2】:

您没有将 open 函数分配给相应的 handlersopen 正在返回文件类型的对象)。

filename = str(file_in)
file_out = create_filename_for_fileout(filename)

open(file_in, 'r')
open(file_out, 'w')

应该是:

file_out = open(create_filename_for_fileout(file_in), 'w')
file_in = open(file_in, 'r')

注意:为清楚起见,最好为 infile handler 使用另一个指针。

检查:https://docs.python.org/2/library/functions.html#open

打开(名称[,模式[,缓冲]])

Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised.

【讨论】:

    猜你喜欢
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2015-09-02
    • 2011-10-04
    • 2021-02-27
    • 2013-03-04
    相关资源
    最近更新 更多