0
# 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."

But even though i have pasted my file in the directory above what can possibly go wrong please tell me?

this is following error code

C:\Python27\python.exe "C:/Users/Lisbeth Salander/Desktop/python/practice.py"
File not found.
C:\Users\Lisbeth Salander\Desktop\python
False

Process finished with exit code 0

P.s I use python 2.7, I have commented my code for easy understanding , if there are any more suggestion, let me know it will be appreciated. I am creating a tool to read phone number from one static file

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    The string literal use '\' simbol as escape simbol. You need to replace '\' with '\\' in path to your txt file in your script. Like this "C:\\Users\\Lisbeth Salander\\Desktop\\python\\randomtext.txt" – oklas Jan 27 '17 at 02:38
  • Please leave feedback if that helpfull. – oklas Jan 27 '17 at 02:41
  • 2
    Possible duplicate of [Windows path in python](http://stackoverflow.com/questions/2953834/windows-path-in-python) – Håken Lid Jan 27 '17 at 03:02

0 Answers0