当前位置:   article > 正文

python中filenotfounderror_python - FileNotFoundError: [Errno 2] No such file or directory:

python filenotfounderror: [errno 2] no such file or directory

count = 0

answer = ""

pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}

name = ""

def program_start():

answer = input('Do you want to make some TXT with the same passwords? y\\n :')

count = int(input('How many TXT files do you want to make?'))

name = input('Enter the hot user id:')

name = name+".TXT"

password_collector() # collect password to pass_pool dictionary

create_file() #create TXT file. it has to be in capital "TXT"

#for the safe program.

def create_file():

newTXT = open(name, "w")

newTXT.write(name + "\n \n" )

for system , password in pass_pool.items():

newTXT.write(system + ":" + password )

newTXT.close()

I get:

File "C:\Python33\mypy\txt creator.py", line 16, in create_file

newTXT = open(name, "w")

FileNotFoundError: [Errno 2] No such file or directory:

From what I look on google this error mean wrong path or file not found. But I check with sys.path and saw that "C:\Python33\mypy" in my paths, and I create the file with "w" so it should work with no problems.

When I used only the create_file() function in the shell it works with no problem.

python

file

createfile

|

this question

edited Feb 15 at 10:33

rrao 137 1 11 asked Sep 9 '13 at 6:04

Or Halimi 176 1 3 14 Use raw strings or escape the '\\', otherwise '\t' is treated as tab character:

r"C:\Python33\mypy\txt creator.py" –

Ashwini Chaudhary Sep 9 '13 at 6:08

|

1 Answers

1

---Accepted---Accepted---Accepted---

When you set the value of name in program_start, Python creates a variable name local to that function's scope, which masks the global name, so the global value remains unchanged. In create_file you use the unchanged global name, which equals to "", and opening a file with the name "" gives you an error.

The quick-and-dirty fix would be addingwith open('C:\\Users\\Ben.Brittany-PC\\Documents\\stats.txt', 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in reader: print(row) and the error: FileNotFoundError: [Errno 2] No such f

global name

in the beginning of program_start. But it is much clearer to write

count = 0

answer = ""

pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}

def program_start():

answer = input('Do you want to make some TXT with the same passwords? y\\n :')

count = int(input('How many TXT files do you want to make?'))

name = input('Enter the hot user id:')

name = name+".TXT"

password_colector() # collect password to pass_pool dic

create_file(name) #create TXT file. it has to be in capital "TXT"

#for the safe pogram.

def create_file(name):

newTXT = open(name, "w")

newTXT.write(name + "\n \n" )

for system , password in pass_pool.items():

newTXT.write(system + ":" + password )

newTXT.close()

|

this answer answered Sep 9 '13 at 6:16

fjarri 6,808 17 34 ho thats right! i totaly miss that. so i will get problem with count and answer to.. thx alot! –

Or Halimi Sep 9 '13 at 6:25

|

de (it's just a simple code but I cannot solve the problem): import csvwith open('address.csv','r') as f: reader = csv.reader(f) for row in reader: print row python file find share edited Mar 9 '14 at 13:28 embert 3,337 3

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/963835
推荐阅读
相关标签
  

闽ICP备14008679号