Python open file with forward slash

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)?

If using backslash, because it is a special character in Python, you must remember to escape every instance: 'C:\\Users\\narae\\Desktop\\alice.txt' ,Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\alice.txt'. That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash. ,Python lets you use OS-X/Linux style slashes "/" even in Windows. Therefore, you can refer to the file as 'C:/Users/narae/Desktop/alice.txt'. RECOMMENDED. ,In a Python script: When you execute your script, your CWD is set to the directory where your script is. Therefore, you can refer to a file in a script by its name only provided that the file and the script are in the same directory. An example: myfile = open('alice.txt') # alice.txt is in the same dir as foo.py mytxt = myfile.read() myfile.close() foo.py

>>> myfile = open('C:/Users/narae/Desktop/alice.txt') # Windows >>>
   mytxt = myfile.read() >>>
   myfile.close()

>>> myfile = open('/Users/narae/Desktop/alice.txt') # Mac and Linux >>>
   mytxt = myfile.read() >>>
   myfile.close()

myfile = open('alice.txt') # alice.txt is in the same dir as foo.py
mytxt = myfile.read()
myfile.close()
foo.py

>>>
import os
   >>>
   os.getcwd()
'D:\\Lab' >>>
os.chdir('scripts/gutenberg') # relative path: scripts dir is under Lab >>>
   os.getcwd()
'D:\\Lab\\scripts\\gutenberg' >>>
os.chdir(r 'D:\Corpora\corpus_samples') # absolute path, using\ and r prefix >>>
   os.getcwd()
'D:\\Corpora\\corpus_samples'


C and D are somewhat better, since they use string formatting, but they still do not resolve the system-dependence problem. If I apply the result under Windows, I get a functional, but inconsistent path with a mixture of separators.,Simply avoid the Windows separator and instead write the path using Linux separators only:,The interpreter doesn’t understand the character sequence \U, since this initiates Unicode characters of a similar sequence. This problem arises because the Windows system uses the backslash “\” as a path separator and Linux uses the slash “/”. Unfortunately, since the Windows separator is also the initiator for diverse special characters or escape in Unicode, it obviously confuses everything. Just like we don’t expect any coherence soon in the use of decimal separators in various countries, our only choice is to go for one of three solutions.,Assuming that you wish to get a listing of a particular path accurately, we start by selecting a user directory on a Windows 10 system, which is basically a reproducible example:

Assuming that you wish to get a listing of a particular path accurately, we start by selecting a user directory on a Windows 10 system, which is basically a reproducible example:

path_dir: str = "C:\Users\sselt\Documents\blog_demo"

The variables assigned upon execution immediately cause an error:

SyntaxError: (unicode error)
'unicodeescape'
codec can 't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Simply avoid the Windows separator and instead write the path using Linux separators only:

path_dir: str = "C:/Users/sselt/Documents/blog_demo"


Then, we used the os.chdir() function to change the current working directory. Note that when we called the os.chdir() function, we used a Linux-style pathname (forward slashes, no drive letter) even though we're on Windows. This is one of the places where Python tries to paper over the differences between operating systems.,Combining these techniques, we can easily construct pathnames for directories and files in the user's home directory. The os.path.join() function can take any number of arguments. ,The directory path uses a forward slash without mentioning operating system. Windows uses backward slashes to denote subdirectories, while Linux use forward slashes. But in Python, forward slashes always work, even on Windows.,However, a binary stream object has no encoding attribute. That's because we're reading bytes, not strings, so there's no conversion for Python to do.

The current working directory is a property that Python holds in memory at all times. There is always a current working directory, whether we're in the Python Shell, running our own Python script from the command line, etc.

>>>
import os
   >>>
   print(os.getcwd())
C: \Python32 >>>
   os.chdir('/test') >>>
   print(os.getcwd())
C: \test

os.path contains functions for manipulating filenames and directory names.

>>>
import os
   >>>
   print(os.path.join('/test/', 'myfile')) /
   test / myfile >>>
   print(os.path.expanduser('~'))
C: \Users\ K >>>
   print(os.path.join(os.path.expanduser('~'), 'dir', 'subdir', 'k.py'))
C: \Users\ K\ dir\ subdir\ k.py

Note: we need to be careful about the string when we use os.path.join. If we use "/", it tells Python that we're using absolute path, and it overrides the path before it:

>>>
import os
   >>>
   print(os.path.join('/test/', '/myfile')) /
   myfile


Python's solution is using raw string with a prefix r in the form of r'...'. It ignores interpretation of the Python's string escape sequence. For example, r'\n' is '\'+'n' (two characters) instead of newline (one character). Using raw string, you can write r'\d+' for regex pattern \d+ (instead of regular string '\\d+').,For simple text string operations such as string search and replacement, you can use the built-in string functions (e.g., str.replace(old, new)). For complex pattern search and replacement, you need to master regular expression (regex).,On the other hand, Python' regular strings also use backslash for escape sequences, e.g., \n for newline, \t for tab. Again, you need to write \\ for \.,To write the regex pattern \d+ (one or more digits) in a Python regular string, you need to write '\\d+'. This is cumbersome and error-prone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
file_copy: Copy file line-by-line from source to destination
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage: file_copy <src> <dest>
"""
import sys
import os

def main():
    # Check and retrieve command-line arguments
    if len(sys.argv) != 3:
        print(__doc__)
        sys.exit(1)   # Return a non-zero value to indicate abnormal termination
    fileIn  = sys.argv[1]
    fileOut = sys.argv[2]

    # Verify source file
    if not os.path.isfile(fileIn):
        print("error: {} does not exist".format(fileIn))
        sys.exit(1)

    # Verify destination file
    if os.path.isfile(fileOut):
        print("{} exists. Override (y/n)?".format(fileOut))
        reply = input().strip().lower()
        if reply[0] != 'y':
           sys.exit(1)

    # Process the file line-by-line
    with open(fileIn, 'r') as fpIn, open(fileOut, 'w') as fpOut:
        lineNumber = 0
        for line in fpIn:
            lineNumber += 1
            line = line.rstrip()   # Strip trailing spaces and newline
            fpOut.write("{}: {}\n".format(lineNumber, line))
            # Need \n, which will be translated to platform-dependent newline
        print("Number of lines: {}\n".format(lineNumber))

if __name__ == '__main__':
    main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
file_list_oswalk.py - List files recursively from a given directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage: files_list_oswalk.py [<dir>|.]
"""
import sys
import os

def main():
    # Process command-line arguments
    if len(sys.argv) > 2:  # Command-line arguments are kept in a list 'sys.argv'
        print(__doc__)
        sys.exit(1)        # Return a non-zero value to indicate abnormal termination
    elif len(sys.argv) == 2:
        dir = sys.argv[1]  # directory given in command-line argument
    else:
        dir = '.'          # default current directory

    # Verify dir
    if not os.path.isdir(dir):
        print('error: {} does not exists'.format(dir))
        sys.exit(1)

    # Recursively walk thru from dir using os.walk()
    for curr_dir, subdirs, files in os.walk(dir):
        # os.walk() recursively walk thru the given "dir" and its sub-directories
        # For each iteration:
        #    - curr_dir: current directory being walk thru, recursively from "dir"
        #    - subdirs: list of sub-directories in "curr_dir"
        #    - files: list of files/symlinks in "curr_dir"
        print('D:', os.path.abspath(curr_dir))    # print currently walk dir
        for subdir in sorted(subdirs):  # print all subdirs under "curr_dir"
            print('SD:', os.path.abspath(subdir))
        for file in sorted(files):      # print all files under "curr_dir"
            print(os.path.join(os.path.abspath(curr_dir), file))  # full filename

if __name__ == '__main__':
    main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
file_list_glob.py - List files recursively from a given directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage: files_list [<dir>|.]
"""
import sys
import os
import glob  # Python 3.5

def main():
    # Process command-line arguments
    if len(sys.argv) > 2:  # Command-line arguments are kept in a list 'sys.argv'
        print(__doc__)
        sys.exit(1)        # Return a non-zero value to indicate abnormal termination
    elif len(sys.argv) == 2:
        dir = sys.argv[1]  # directory given in command-line argument
    else:
        dir = '.'          # default current directory

    # Check dir
    if not os.path.isdir(dir):
        print('error: {} does not exists'.format(dir))
        sys.exit(1)

    # List *.txt only
    for file in glob.glob(dir + '/**/*.txt', recursive=True):
        # ** match any files and zero or more directories and subdirectories
        print(file)

    print('----------------------------')

    # List all files and subdirs
    for file in glob.glob(dir + '/**', recursive=True):
        # ** match any files and zero or more directories and subdirectories
        if os.path.isdir(file):
            print('D:', file)
        else:
            print(file)

if __name__ == '__main__':
    main()


need to escape them by doubling:

wbkName = '\\AA\\chart.xlsx'

of the escape character processing:

wbkName = r '\AA\chart.xlsx'

forward slashes, since Windows will accept them as well:

wbkName = '/AA/chart.xlsx'


On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.,Figure 8-4. The base name follows the last slash in a path and is the same as the filename. The dir name is everything before the last slash.,Figure 8-2 is an example of some folders and files. When the current working directory is set to C:\bacon, the relative paths for the other folders and files are set as they are in the figure.,You can determine whether there is a DVD or flash drive currently attached to the computer by checking for it with the os.path.exists() function. For instance, if I wanted to check for a flash drive with the volume named D:\ on my Windows computer, I could do that with the following:

>>>
import os
   >>>
   os.path.join('usr', 'bin', 'spam')
'usr\\bin\\spam'

>>>
import os
   >>>
   os.getcwd()
'C:\\Python34' >>>
os.chdir('C:\\Windows\\System32') >>>
   os.getcwd()
'C:\\Windows\\System32'

>>>
import os
   >>>
   os.makedirs('C:\\delicious\\walnut\\waffles')

>>> os.path.abspath('.')
'C:\\Python34' >>>
os.path.abspath('.\\Scripts')
'C:\\Python34\\Scripts' >>>
os.path.isabs('.')
False
   >>>
   os.path.isabs(os.path.abspath('.'))
True

>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
776192
   >>>
   os.listdir('C:\\Windows\\System32')['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
      --snip--
      'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']

>>> os.path.exists('C:\\Windows')
True
   >>>
   os.path.exists('C:\\some_made_up_folder')
False
   >>>
   os.path.isdir('C:\\Windows\\System32')
True
   >>>
   os.path.isfile('C:\\Windows\\System32')
False
   >>>
   os.path.isdir('C:\\Windows\\System32\\calc.exe')
False
   >>>
   os.path.isfile('C:\\Windows\\System32\\calc.exe')
True


How do you do a forward 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. When specifying a path, a forward slash (/) can be used in place of a backslash.

How do I make a file into a slash?

Use a period to separate the base file name from the extension in the name of a directory or file. Use a backslash ( \ ) to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path.

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.

Why can't you use a slash in a file name?

These characters are prevented from being used in file names because it would confuse the system when trying to access the file. When Apple developed OS X, the use of the forward slash was adopted because of its Unix underpinnings, so in OS X you cannot include a true forward slash in a file name.