Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape forward slashes in python, so that open() sees my file as a filename to write, instead of a filepath to read?

Let me preface this by saying I'm not exactly sure what is happening with my code; I'm fairly new to programming.

I've been working on creating an individual final project for my python CS class that checks my teacher's website on a daily basis and determines if he's changed any of the web pages on his website since the last time the program ran or not.

The step I'm working on right now is as follows:

def write_pages_files():
    '''
    Writes the various page files from the website's links 
    '''
    links = get_site_links()
    for page in links:
        site_page = requests.get(root_url + page)
        soup = BeautifulSoup(site_page.text)
        with open(page + ".txt", mode='wt', encoding='utf-8') as out_file:
            out_file.write(str(soup))

The links look similar to this:

/site/sitename/class/final-code

And the error I get is as follows:

with open(page + ".txt", mode='wt', encoding='utf-8') as out_file:
FileNotFoundError: [Errno 2] No such file or directory: '/site/sitename/class.txt'

How can I write the site pages with these types of names (/site/sitename/nameofpage.txt)?

like image 544
cody.codes Avatar asked Mar 12 '15 15:03

cody.codes


People also ask

How do you escape the front slash in Python?

Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab.

How do you escape a forward slash?

In cases where you either cannot or prefer not to use alternate delimiters, you can escape the forward slashes with a backslash: m/\/[^/]+$/ for example (using an alternate delimiter that could become m{/[^/]+$} , which may read more clearly).

How do you escape a character in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.


3 Answers

you cannot have / in the file basename on unix or windows, you could replace / with .:

page.replace("/",".") + ".txt"

Python presumes /site etc.. is a directory.

like image 172
Padraic Cunningham Avatar answered Oct 17 '22 14:10

Padraic Cunningham


On Unix/Mac OS, for the middle slashes, you can use : which will convert to / when viewed, but trigger the subfolders that / does.

site/sitename/class/final-code -> final-code file in a class folder in a sitename folder in a site folder in the current folder site:sitename:class:final-code -> site/sitename/class/final-code file in the current folder.

like image 33
Fleija Avatar answered Oct 17 '22 12:10

Fleija


Related to the title of the question, though not the specifics, if you really want your file names to include something that looks like a slash, you can use the unicode character "∕" (DIVISION SLASH), aka u'\u2215'.

This isn't useful in most circumstances (and could be confusing), but can be useful when the standard nomenclature for a concept you wish to include in a filename includes slashes.

like image 5
RecursivelyIronic Avatar answered Oct 17 '22 14:10

RecursivelyIronic