"There is no such file or directory" from os.mkdir

works on a python project, and he does this by looking at the lifehacker.com index, and then finds all the tags with the class "h5 hover-highlight entry-title", then he creates the 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 Perspective (Updated: 1/5/2015)"

help will be great, thanks!

heres my atm code:

 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!" 
+5
source share
1 answer

Sanitize your file name. (Failure to do so will also lead to security problems, especially if you are not preventing you from working with ../ ).

It could be simple:

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

Be that as it may, 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 Perspective (Updated: 1 Since none of them exist, and os.mkdir() not recursive, you get an error message. (If you want a recursive operation, see os.makedirs() ).

+4
source

Source: https://habr.com/ru/post/1210631/


All Articles