Reputation: 21
I have a script that access an url and retrieve data like this:
usock = urllib2.urlopen('http://www.google.com.br')
#Reads the page
data = usock.read()
usock.close()
#Search for links
links = re.findall('"((http)s?://.*?)"', data)
And I would like to use a txt file as source for urls, I already have a txt file with urls by line like this:
http://www.google.com.br/
http://www.recrutamento.com.br/
I was trying to use this file with this code, but I always get IOError: [Errno 2] No such file or directory:
here is where I get stuck:
os.chdir("C:\Python27")
urls = open(os.path.join(os.getcwd(), 'ord.txt'), 'r').readlines()
for url in urls:
usock = urllib2.urlopen(url)
#Reads the page
data = usock.read()
usock.close()
It displays exactly the correct path and file name:
IOError: [Errno 2] No such file or directory: C:\\Python27\\ord.txt
Upvotes: 0
Views: 86
Reputation: 20126
You have a typo:
os.chdir("C:\\Python27")
or
os.chdir(r"C:\Python27")
will solve it (notice the double backslash \\
in the first option and the r
prefix in the second one).
Explanation: You can read here http://docs.python.org/2/reference/lexical_analysis.html#string-literals about escape sequences in Python.
Upvotes: 1