我确信之前已经回答了,但我找不到任何可以帮助我...
我正在尝试编写一个简单的程序来读取文件并搜索一个单词,然后打印该文件中找到该单词的次数.好吧,每当我输入"test.rtf"(这是我的文档的名称)时,我都会收到此错误...
Traceback (most recent call last):
File "/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/How many? (Python).py", line 9, in <module>
fileScan= open(fileName, 'r') #Opens file
FileNotFoundError: [Errno 2] No such file or directory: 'test.rtf'
Run Code Online (Sandbox Code Playgroud)
在上学期的课堂上,我似乎记得我的教授说你必须将文件保存在特定的地方?我不确定他是否真的这么说,但是如果有帮助的话,我正在运行苹果OSx.哈哈
这是我的代码,任何帮助表示赞赏:)提前致谢!
print ("Hello! Welcome to the 'How many' program.")
fileName= input("Please enter the name of the file you'd like to use. Make \
sure to include the correct extension!") #Gets file name
fileScan= open(fileName, 'r') #Opens file
cont = "Yes"
accumulator = 0
while cont == "Yes": …Run Code Online (Sandbox Code Playgroud) 我试图遍历这样的目录中的文件:
import os
path = r'E:/somedir'
for filename in os.listdir(path):
f = open(filename, 'r')
... # process the file
Run Code Online (Sandbox Code Playgroud)
但是,FileNotFoundError即使文件存在,Python仍会抛出:
Traceback (most recent call last):
File "E:/ADMTM/TestT.py", line 6, in <module>
f = open(filename, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'foo.txt'
Run Code Online (Sandbox Code Playgroud)
那么,这里出了什么问题?
我正在编写一个数据库程序,personica 是我的测试对象(我通常会在文件路径的位置使用一个变量,但出于测试和演示目的,我只有一个字符串。)。在我的计算机上的这个确切位置有一个文本文件(顺便说一下,我在这里更改了我的用户名,因为我是偏执狂。),但它说:
Traceback (most recent call last):
File "C:\Users\Admin\Documents\Project
Documentation\InteractiveExecutable.py", line 46, in <module>
ReadPerson = open("C:/Users/Admin/Documents/Project
Documentation/personica.txt", 'r')
IOError: [Errno 2] No such file or directory:
'C:/Users/Admin/Documents/Project Documentation/personica.txt'
Run Code Online (Sandbox Code Playgroud)
这是代码行:
ReadPerson = open("C:/Users/Admin/Documents/Project Documentation/personica.txt", 'r')
Run Code Online (Sandbox Code Playgroud)
我确信它就在那里,当我将该地址复制到 Windows 资源管理器时,它会将我带到文本文件。
有谁知道为什么这不起作用?
目前,我正在尝试使用python中的csv模块读取csv文件。当我运行下面的代码时,我得到一个错误,指出该文件不存在。我的第一个猜测是,可能我将文件保存在错误的位置,或者需要为pyton提供文件路径。目前,我已将文件保存在C:\ Documents and Settings \ eag29278 \ My Documents \ python测试代码\ test_satdata.csv中。
一侧说明,请注意,将模式设置为“ rb”(读取二进制)是正确的做法。
import csv
with open('test_satdata.csv', 'rb') as csvfile:
satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
for row in satreader:
print ', '.join(row)
Run Code Online (Sandbox Code Playgroud)
这是错误代码。
Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'
Run Code Online (Sandbox Code Playgroud) 这是执行的截图:
如您所见,错误显示目录"JSONFiles/Apartment/Rent/dubizzleabudhabiproperty"不存在.
但是看看我的文件,请:
文件夹肯定在那里.
代码
self.file = open("JSONFiles/"+ item["category"]+"/" + item["action"]+"/"+ item['source']+"/"+fileName + '.json', 'wb') # Create a new JSON file with the name = fileName parameter
line = json.dumps(dict(item)) # Change the item to a JSON format in one line
self.file.write(line) # Write the item to the file
Run Code Online (Sandbox Code Playgroud)
当我将文件名更改为较小的文件名时,它可以正常工作,因此问题在于路径的长度.请问是什么解决方案?
我正在尝试学习如何在Python中解析.txt文件.这导致我打开解释器(终端> python)并玩游戏.但是,我似乎无法指定正确的路径.Python首先在哪里看?
这是我的第一步:
f = open("/Desktop/temp/myfile.txt","file1")
Run Code Online (Sandbox Code Playgroud)
这显然是行不通的.任何人都可以建议吗?
我写了一个小模块,它首先找到目录中的所有文件,然后合并它们。但是,我在从目录中打开这些文件时遇到了问题。我确保我的文件和目录名称是正确的,并且文件实际上在目录中。
下面是代码..
seqdir = "results"
outfile = "test.txt"
for filename in os.listdir(seqdir):
in_file = open(filename,'r')
Run Code Online (Sandbox Code Playgroud)
下面是错误..
in_file = open(filename,'r')
IOError: [Errno 2] No such file or directory: 'hen1-1-rep1.txt'
Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本来读取一个 csv 文件。csv 文件和脚本位于同一目录中。但是当我尝试打开文件时,它给了我FileNotFoundError: [Errno 2] No such file or directory: 'zipcodes.csv'. 我用来读取文件的代码是
with open('zipcodes.csv', 'r') as zipcode_file:
reader = csv.DictReader(zipcode_file)
Run Code Online (Sandbox Code Playgroud)
如果我提供文件的完整路径,它将起作用。为什么open()需要文件的完整路径?
我有这个代码:
import os.path
import numpy as np
homedir=os.path.expanduser("~")
pathset=os.path.join(homedir,"\Documents\School Life Diary\settings.npy")
if not(os.path.exists(pathset)):
ds={"ORE_MAX_GIORNATA":5}
np.save(pathset, ds)
Run Code Online (Sandbox Code Playgroud)
但他给我的错误是:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Maicol\\Documents\\School Life Diary\\settings.npy'
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?文件夹未创建...
谢谢
当我通过 Visual Studio Code 加载文件时,VScode 找不到目录。
我可以在没有终端问题的情况下运行代码,结果是:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot$ python text_load_text.py
['fij\n', 'feijfaef\n', 'ef\n', 'awef\n', 'awe\n', 'g\n', 'aweg\n', 'ae\n', 'wg\n', 'awe\n', 'h\n', 'aw\n', 'h\n', 'aw\n', 'ef\n', 'aweg\n', 'wea\n', 'gaw\n', 'eg\n', '\n']
Run Code Online (Sandbox Code Playgroud)
但是使用 VScode:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials$ cd /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /home/young/anaconda3/bin/python /home/young/.vscode/extensions/ms-python.python-2018.6.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials 39707 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py
Traceback (most recent call last):
File "/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py", line 8, in <module>
with open('test.txt', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Run Code Online (Sandbox Code Playgroud)
什么原因导致这个问题?
test.txt 文件原本打算位于 ./data/test.txt
所以我测试了
with open('./data/test.txt', 'r') as …Run Code Online (Sandbox Code Playgroud)