import os
import random
os.chdir("C:\Users\Mainuser\Desktop\Lab6")
#Am i supposed to have a os.chdir?
# I think this is what's giving the error
#how do i fix this?
def getDictionary():
result = []
f = open("pocket-dic.txt","r")
for line in f:
result = result + [ line.strip() ];
return result
def makeText(dict, words=50):
length = len(dict)
for i in range(words):
num = random.randrange(0,length)
words = dict[num]
print word,
if (i+1) % 7 == 0:
print
Run Code Online (Sandbox Code Playgroud)
Python给我一个错误,说它无法找到指定的路径,当我在桌面上清楚地有一个带有该名称的文件夹时.它可能是os.chidr ?? 我究竟做错了什么?
概要:如何在Python中读取文件?为什么一定要这样做?
我的问题是我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Terminal\Desktop\wkspc\filetesting.py", line 1, in <module>
testFile=open("test.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Run Code Online (Sandbox Code Playgroud)
其源自以下代码:(即整个'.py'文件)
testFile=open("test.txt")
print(testFile.read())
Run Code Online (Sandbox Code Playgroud)
"test.txt"与我的程序位于同一文件夹中.我是Python的新手,不明白为什么我会收到文件位置错误.我想知道修复以及必须以这种方式完成修复的原因.
我试过使用文件的绝对路径,"C:\ Users\Terminal\Desktop\wkspc\test.txt"
其他详情:
"Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32"
Windows 7, 32 Bit
Run Code Online (Sandbox Code Playgroud) 我的书说:
在您的计算机上运行的每个程序都有一个当前工作目录或 cwd。任何不以根文件夹开头的文件名或路径都假定在当前工作目录下
因为我在 OSX 上,所以我的根文件夹是 /。当我在os.getcwd()Python shell 中输入时,我得到/Users/apple/Documents. 为什么我的 cwd 中出现 Documents 文件夹?是说 Python 正在使用 Documents 文件夹吗?没有任何以/( 根文件夹 )开头的指向 Python 的路径吗?另外,每个程序都有不同的 cwd 吗?
我正在通过python中的uart处理文件的传输文件夹.下面你看到简单的函数,但是有一个问题,因为我得到错误,如标题:IOError:[Errno 2]没有这样的文件或目录:'1.jpg'其中1.jpg是测试文件夹中的文件之一.所以这很奇怪,因为程序知道它不存在的文件名?我做错了什么?
def send2():
path = '/home/pi/Downloads/test/'
arr = os.listdir(path)
for x in arr:
with open(x, 'rb') as fh:
while True:
# send in 1024byte parts
chunk = fh.read(1024)
if not chunk: break
ser.write(chunk)
Run Code Online (Sandbox Code Playgroud) 我试图用python从XML文件中提取数据.我尝试了以下代码.
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("data_v2.xml")
Run Code Online (Sandbox Code Playgroud)
错误信息:
IOError: [Errno 2] No such file or directory: 'data_v2.xml'.
Run Code Online (Sandbox Code Playgroud) 首先,我对Python和编程很新.
目前我正在尝试创建一个脚本,根据黑名单中的行删除文件夹中随机名称,扩展名和内容的所有文件(搜索必须在文件内容中完成).
这是一个代码:
import os
black_list = [line for line in open("C:/path/to/blacklist.txt")]
for filename in os.listdir("C:/path/to/files/"):
content = open(filename).read()
if any(line in content for line in black_list):
os.remove(filename)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
IOError:[Errno 2]没有这样的文件或目录:'first_file_from_the_folder'
请问你能帮帮我吗?
提前致谢!
我正在尝试从文件夹中打开文件并读取它,但是找不到它。我正在使用Python3
这是我的代码:
import os
import glob
prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-
codes/Mayur_Python_code/Question/wx_data/"
target_path = open('MissingPrcpData.txt', 'w')
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if
f.endswith('.txt')]
file_array.sort() # file is sorted list
for f_obj in range(len(file_array)):
file = os.path.abspath(file_array[f_obj])
join_file = os.path.join(prefix_path, file) #whole file path
for filename in file_array:
log = open(filename, 'r')#<---- Error is here
Run Code Online (Sandbox Code Playgroud)
Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'
我有一个小问题,我编写的python程序是从一个特殊的文本文件中提取一些信息.循环(下面的代码)需要执行我的函数extract_zcoords()超过500个文件(1个文件给出一个列表),以便我可以构建数据集.
import os
def extract_zcoord(filename):
f = open(filename, 'r')
... # do something with f
### LOOP OVER DIRECTORY
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
extract_zcoord(filename)
Run Code Online (Sandbox Code Playgroud)
错误:IOException No such file or directory是发生的,因此由于某种原因python不访问文件.我检查了目录路径名(位置)和文件权限,它们是正确的(读取+写入).当文件存在且路径名是否正确时,为什么会报告IOError的任何想法?
什么想法可能是错的?
帮我解决问题。data.json ' 与我的 python 脚本在同一目录中,但是当我运行程序时出现以下错误
发生异常:FileNotFoundError [Errno 2] 没有这样的文件或目录:'data.json'
import json
data = json.load(open("data.json))
def translate(w):
return data[w]
word = input("Enter word: ")
print(translate(word))
Run Code Online (Sandbox Code Playgroud)