Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Don';我不明白为什么会发生“找不到此文件”错误_Python_Python 2.7 - Fatal编程技术网

Python Don';我不明白为什么会发生“找不到此文件”错误

Python Don';我不明白为什么会发生“找不到此文件”错误,python,python-2.7,Python,Python 2.7,但即使我已将文件粘贴到上面的目录中 有什么可能出错请告诉我 这是下面的错误代码 # filename variables import os import re filename = 'randomtext.txt' newfilename = 'result.txt' # read the file if os.path.exists(filename): data = open(filename, 'r') phonenumber = data.read() else:

但即使我已将文件粘贴到上面的目录中 有什么可能出错请告诉我

这是下面的错误代码

# filename variables
import os
import re

filename = 'randomtext.txt'
newfilename = 'result.txt'

# read the file
if os.path.exists(filename):
    data = open(filename, 'r')
    phonenumber = data.read()
else:
    print "File not found."
    print (os.getcwd())
    print os.path.isfile("C:\Users\Lisbeth Salander\Desktop\python\randomtext.txt")
    raise SystemExit

# regex = regular expression to remove extract indian phone numbers
# It will remove counry code of any indian number eg: +91-9818249892 will become 9818249892
# the lenght number should be 1o digits only

r = re.compile(r'(!(\!+?\![91]?\-?))?\d{10}')
results = r.findall(phonenumber)
extracted_phonenumber = ""
for x in results:
    extracted_phonenumber += str(x) + "\n"


# function to write file
def writefile():
    f = open(newfilename, 'w')
    f.write(extracted_phonenumber)
    f.close()


# removing duplicates

content = open(newfilename, 'r').readline()
content_set = set(content)
cleandata = open('clean_result.txt', 'w')

for line in content_set:
    cleandata.write(line)

print "File written."
进程已完成,退出代码为0


另外,我使用Python2.7,为了便于理解,我已经对我的代码进行了注释,如果还有任何建议,请告诉我,我将不胜感激。我正在创建一个从一个静态文件读取电话号码的工具

字符串文字使用“\”simbol作为转义simbol。您需要将脚本中txt文件路径中的“\”替换为“\”。如“C:\\Users\\Lisbeth Salander\\Desktop\\python\\randomtext.txt”所示,如果该帮助信息完整,请留下反馈。可能重复的
C:\Python27\python.exe "C:/Users/Lisbeth Salander/Desktop/python/practice.py"
File not found.
C:\Users\Lisbeth Salander\Desktop\python
False