# Folder Path
path = "/content/gdrive/MyDrive/data files"
# Change the directory
os.chdir(path)
# Read text File
def read_text_file(file_path):
with open(file_path, 'r') as f:
info=f.read()
stop_words(info)
# iterate through all file
for file in os.listdir():
# Check whether file is in text format or not
if file.endswith(".txt"):
file_path = f"{path}{file}"
# call read text file function
read_text_file(file_path)
我得到这个错误:
FileNotFoundError Traceback (most recent call last)
<ipython-input-139-07ff61fe2c38> in <module>()
22
23 # call read text file function
---> 24 read_text_file(file_path)
<ipython-input-139-07ff61fe2c38> in read_text_file(file_path)
10
11 def read_text_file(file_path):
---> 12 with open(file_path, 'r') as f:
13 info=f.read()
14 stop_words(info)
FileNotFoundError: [Errno 2] No such file or directory: '/content/gdrive/MyDrive/data files\URL_ID_1.txt'
这可能是由于linux, windows不一致,您应该添加您的文件路径
file_path = os.path.join(路径,文件)