Python: 如何将带有文件名的字符串转换为可读的文件名?
Python: how to transform a string which carries the filename into a readable filename?
我想将文件循环到目录中,对这些文件进行一些处理,然后为每个文件写出结果。
但是无法读取我的文件,因为 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'
>>>
您没有将 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")
您没有将 open
函数分配给相应的 handlers
(open
正在返回文件类型的对象)。
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
.[=19 使用另一个指针=]
检查:https://docs.python.org/2/library/functions.html#open
open(name[, mode[, buffering]])
Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised.
我想将文件循环到目录中,对这些文件进行一些处理,然后为每个文件写出结果。 但是无法读取我的文件,因为 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'
>>>
您没有将 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")
您没有将 open
函数分配给相应的 handlers
(open
正在返回文件类型的对象)。
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
.[=19 使用另一个指针=]
检查:https://docs.python.org/2/library/functions.html#open
open(name[, mode[, buffering]])
Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised.