我在读取用另一种方法创建的日志文件时遇到问题。 日志文件中有解压文件的文件路径(解包在另一个函数中)。日志文件看起来像这样
/home/usr/Downloads/outdir/Code/XXX.something
/home/usr/Downloads/outdir/Code/YYY.something
/home/usr/Downloads/outdir/Code/AAA.something
@staticmethod
def iterate_log(path):
results = str()
for dirpath, dirnames, files in os.walk(path):
for name in files:
results += '%s\n' % os.path.join(dirpath, name)
with open(os.path.join(EXTRACT_PATH_, LOGNAME_), 'w') as logfile:
logfile.write(results)
return os.path.join(EXTRACT_PATH_, LOGNAME_)
@staticmethod
def read_received_files(from_file):
with open(from_file, 'r') as data:
data = data.readlines()
for lines in data:
# to reduce confusion I would use
# for line in data:
read_files = open(lines)
print(read_files)
return read_files
现在我想逐行读取日志文件(第二个方法中的参数from_files
是第一个方法的返回值),打开这些文件并返回它们(在其他地方使用它们)。
readlines()
和read()
到目前为止都给了我错误
readlines() = [Errno2] No sucht file or directory: '/../../../logfile.log\n
read() = IsADirectoryError: [Errno21]
全回溯:
Traceback (most recent call last):
File "files_received_test.py", line 13, in <module>
main()
File "files_received_test.py", line 9, in main
j = Filesystem.execute_code(FILENAME_)
File "/home/usr/PycharmProjects/Projektgruppe/code/src/filesystem_hasher.py", line 16, in execute_code
Filesystem.read_received_files(create_log)
File "/home/usr/PycharmProjects/Projektgruppe/code/src/filesystem_hasher.py", line 54, in read_received_files
read_files = open(lines)
FileNotFoundError: [Errno 2] No such file or directory: '/home/usr/Downloads/outdir/unpacked_files.log\n'
你只需要去掉换行符就可以了
至
作为一个观察结果(对于读取错误消息中的每个字符都很关键),该消息告诉您没有名为
因此,尝试理解\n出现的原因非常有用-这与您对文件名的期望不符,因此您应该想知道为什么文件具有该名称
相关问题 更多 >
编程相关推荐