python os 模块看到文件夹中的项目但无法修改它们
python os module sees items in folder but can't modify them
我正在尝试创建一个循环遍历 .txt 文件列表并将其文件名和内容添加到字典中的程序。程序正确导航到文件夹,我什至可以打印出文件夹中文件的名称。但是,当我尝试将文件名和内容添加到字典时,我在控制台中收到此错误,
Traceback (most recent call last):
File "test.py", line 33, in <module>
user1.append_journal_files(location)
File "test.py", line 21, in append_journal_files
with open(filename, 'r') as file_object:
FileNotFoundError: [Errno 2] No such file or directory: 'dump.0001.txt'
我不确定为什么在我尝试操作这些文件后,这些文件没有被识别为存在。预先感谢大家花时间看这个。如果有任何需要澄清的地方,请告诉我。我是编程新手,想确保我能很好地沟通。再次感谢。
#Python Version 3.6.5
#imports
import os
#classes
class User():
#Class that models a Revit user
def __init__(self, username):
'''initializes the class with a username, additional info
gathered after initialization'''
self.username = username
self.title = ""
self.journal_log = {}
def append_journal_files(self, directory_in_str):
#appends all items of filetype in folder to dictionary instance.
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
with open(filename, 'r') as file_object:
if filename not in self.journal_log.keys():
self.journal_log[filename] = file_object.readlines()
else:
continue
continue
else:
continue
#begin running program
user1 = User("Christian")
location = 'C:\Users\Christian Gentry\AppData\Local\Autodesk\Revit\Autodesk Revit 2018\Journals'
user1.append_journal_files(location)
filename
只是基本名称,例如 dump.0001.txt
.
如果要打开文件,您需要一个完整路径,例如 C:\Users\Christian Gentry\AppData\Local\Autodesk\Revit\Autodesk Revit 2018\Journals\dump0001.txt
。 (它也可以是当前工作目录的相对路径,但它必须是将您带到文件的路径。)
您已经为 listdir
这样做了——您不只是通过了 Journals
并期望它起作用——这里的想法是一样的。
最简单的修复方法是:
pathname = os.path.join(directory, filename)
with open(pathname, 'r') as file_object:
当我们在做的时候:
- 您只转义了 Windows 路径名中的一个反斜杠。从技术上讲,您所拥有的是正确的,因为所有其他路径组件恰好以不是有效反斜杠转义的字母开头,但这确实不是一件好事。使用原始字符串文字、转义所有反斜杠或使用正斜杠。
- 您不需要
fsencode
传递给 listdir
的路径和 fsdecode
您得到的结果。如果你只是传入一个字符串,Python 会自动对它们进行适当的编码并自动为你解码结果。
- 考虑使用
pathlib
而不是 os
;对于新手来说,它更容易理解,它的帮助更容易导航,并且更难出错。在 3.6 之前它不能用于大块的 stdlib,但是你使用的是 3.6,所以这无关紧要。
我正在尝试创建一个循环遍历 .txt 文件列表并将其文件名和内容添加到字典中的程序。程序正确导航到文件夹,我什至可以打印出文件夹中文件的名称。但是,当我尝试将文件名和内容添加到字典时,我在控制台中收到此错误,
Traceback (most recent call last):
File "test.py", line 33, in <module>
user1.append_journal_files(location)
File "test.py", line 21, in append_journal_files
with open(filename, 'r') as file_object:
FileNotFoundError: [Errno 2] No such file or directory: 'dump.0001.txt'
我不确定为什么在我尝试操作这些文件后,这些文件没有被识别为存在。预先感谢大家花时间看这个。如果有任何需要澄清的地方,请告诉我。我是编程新手,想确保我能很好地沟通。再次感谢。
#Python Version 3.6.5
#imports
import os
#classes
class User():
#Class that models a Revit user
def __init__(self, username):
'''initializes the class with a username, additional info
gathered after initialization'''
self.username = username
self.title = ""
self.journal_log = {}
def append_journal_files(self, directory_in_str):
#appends all items of filetype in folder to dictionary instance.
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
with open(filename, 'r') as file_object:
if filename not in self.journal_log.keys():
self.journal_log[filename] = file_object.readlines()
else:
continue
continue
else:
continue
#begin running program
user1 = User("Christian")
location = 'C:\Users\Christian Gentry\AppData\Local\Autodesk\Revit\Autodesk Revit 2018\Journals'
user1.append_journal_files(location)
filename
只是基本名称,例如 dump.0001.txt
.
如果要打开文件,您需要一个完整路径,例如 C:\Users\Christian Gentry\AppData\Local\Autodesk\Revit\Autodesk Revit 2018\Journals\dump0001.txt
。 (它也可以是当前工作目录的相对路径,但它必须是将您带到文件的路径。)
您已经为 listdir
这样做了——您不只是通过了 Journals
并期望它起作用——这里的想法是一样的。
最简单的修复方法是:
pathname = os.path.join(directory, filename)
with open(pathname, 'r') as file_object:
当我们在做的时候:
- 您只转义了 Windows 路径名中的一个反斜杠。从技术上讲,您所拥有的是正确的,因为所有其他路径组件恰好以不是有效反斜杠转义的字母开头,但这确实不是一件好事。使用原始字符串文字、转义所有反斜杠或使用正斜杠。
- 您不需要
fsencode
传递给listdir
的路径和fsdecode
您得到的结果。如果你只是传入一个字符串,Python 会自动对它们进行适当的编码并自动为你解码结果。 - 考虑使用
pathlib
而不是os
;对于新手来说,它更容易理解,它的帮助更容易导航,并且更难出错。在 3.6 之前它不能用于大块的 stdlib,但是你使用的是 3.6,所以这无关紧要。