Table of Contents

Paths to files and directories in Windows and Linux

This info is a complement to the lecture on working with files, please read the lecture first: lec08-files.pdf

Paths are sequences of directory names (possible ended with a file name) which unambiguasly resolve to a directory or a file in the filesystem.

On Linux, the forward slash character / is used as a separator in the paths, e.g.

On Windows, the backword slash, or backslash, character \ is used as a separator in paths, e.g.

But on modern Windows systems you can use in most cases also the forward slashes, as on Linux. I.e. the paths

are valid on Windows as well.

The backslash

In many programming languages, including Python, the backslash in strings is actually interpretted as a so called escape character, i.e. it starts a special character. E.g \n is a new-line character, \t is a tabulator, etc. But how do you type the backslash itself, if the backslash has this special meaning? Well, you use double backslash, \\, which actually represents a single backslash character.

So, if you want to use Windows paths with backslash separators, it will not work directly:

>>> '\users\janedoe\prg\spam'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape

You have several options for specifying paths on Windows:

Joining paths

Very often you have to create a path to a file, given the path to a directory (in which the file resides) and the filename. The best way is to join these parts using os.path.join() function:

>>> import os
>>> corpus_dir = r'\users\janedoe\prg\spam\data'
>>> file_name = '!truth.txt'
>>> os.path.join(corpus_dir, file_name)
'\\users\\janedoe\\prg\\spam\\data\\!truth.txt'