Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No such file or directory" from os.mkdir

working on a python project, and what it does is it looks at the index of lifehacker.com, then finds all tags with the class "headline h5 hover-highlight entry-title", then it creates files for each directory. But the only problem is that when i run it, i get OSError: [Errno 2] No such file or directory: "/home/root/python/The Sony Smartwatch 3: A Runner's Perspective (Updated: 1/5/2015)"

help would be lovely, thanks!

heres my code atm:

import re
import os
import urllib2
from bs4 import BeautifulSoup
from mechanize import Browser

url = "http://lifehacker.com/"
url_open = urllib2.urlopen(url)
soup = BeautifulSoup(url_open.read())

link = soup.findAll("h1",{"class": "headline h5 hover-highlight entry-title"})
file_directory = "/home/root/python/"

for i in link:
    os.mkdir(os.path.join(file_directory, str(i.text)))
    print "Successfully made directory(s)", i.text, "!"
else:
    print "The directory", i.text, "either exists, or there was an error!"
like image 355
DANIEL REAPSOME Avatar asked Jan 08 '15 03:01

DANIEL REAPSOME


1 Answers

Sanitize your filename. (Failing to do so is also going to cause you security issues, particularly if you don't prevent things from starting with ../).

This could be as simple as:

safe_name = i.text.replace('/', '_')
os.mkdir(os.path.join(file_directory, safe_name))

As it is, your code is trying to create a directory named 2015), in a directory named 5, in a directory named The Sony Smartwatch 3: A Runner's Perspective (Updated: 1. Since none of those exist, and os.mkdir() isn't recursive, you get the error in question. (If you want a recursive operation, see os.makedirs() instead).

like image 64
Charles Duffy Avatar answered Sep 30 '22 16:09

Charles Duffy