Python path forward slash or backslash

I am facing a very basic problem using directory path in python script. When I do copy path from the windows explorer, it uses backward slash as path seperator which is causing problem.

>>> x
'D:\testfolder'
>>> print x
D:      estfolder
>>> print os.path.normpath(x)
D:      estfolder
>>> print os.path.abspath(x)
D:\     estfolder
>>> print x.replace('\\','/')
D:      estfolder

Can some one please help me to fix this.

asked Sep 28, 2013 at 8:49

2

Python interprets a \t in a string as a tab character; hence, "D:\testfolder" will print out with a tab between the : and the e, as you noticed. If you want an actual backslash, you need to escape the backslash by entering it as \\:

>>> x = "D:\\testfolder"
>>> print x
D:\testfolder

However, for cross-platform compatibility, you should probably use os.path.join. I think that Python on Windows will automatically handle forward slashes (/) properly, too.

answered Sep 28, 2013 at 8:57

mipadimipadi

385k88 gold badges515 silver badges475 bronze badges

4

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

One of programming’s little annoyances is that Microsoft Windows uses a backslash character between folder names while almost every other computer uses a forward slash:

This is an accident of early 1980’s computer history. The first version of MS-DOS used the forward slash character for specifying command-line options. When Microsoft added support for folders in MS-DOS 2.0, the forward slash character was already taken so they used a backslash instead. Thirty-five years later, we are still stuck with this incompatibility.

If you want your Python code to work on both Windows and Mac/Linux, you’ll need to deal with these kinds of platform-specific issues. Luckily, Python 3 has a new module called pathlib that makes working with files nearly painless.

Let’s take a quick look at the different ways of handling filename paths and see how pathlib can make your life better!

The Wrong Solution: Building File Paths by Hand

Let’s say you have a data folder that contains a file that you want to open in your Python program:

Python path forward slash or backslash

This is the wrong way to code it in Python:

Notice that I’ve hardcoded the path using Unix-style forward slashes since I’m on a Mac. This will make Windows users angry.

Technically this code will still work on Windows because Python has a hack where it will recognize either kind of slash when you call open() on Windows. But even still, you shouldn’t depend on that. Not all Python libraries will work if you use wrong kind of slash on the wrong operating system — especially if they interface with external programs or libraries.

And Python’s support for mixing slash types is a Windows-only hack that doesn’t work in reverse. Using backslashes in code will totally fail on a Mac:

For all these reasons and more, writing code with hardcoded path strings is the kind of thing that will make other programmers look at you with great suspicion. In general, you should try to avoid it.

The Old Solution: Python’s os.path module

Python’s os.path module has lots of tools for working around these kinds of operating system-specific file system issues.

You can use os.path.join() to build a path string using the right kind of slash for the current operating system:

This code will work perfectly on both Windows or Mac. The problem is that it’s a pain to use. Writing out os.path.join() and passing in each part of the path as a separate string is wordy and unintuitive.

Since most of the functions in the os.path module are similarly annoying to use, developers often “forget” to use them even when they know better. This leads to a lot of cross-platform bugs and angry users.

The Better Solution: Python 3’s pathlib!

Python 3.4 introduced a new standard library for dealing with files and paths called pathlib — and it’s great!

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest:

Notice two things here:

  1. You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system. Nice!
  2. If you want to add on to the path, you can use the / operator directly in your code. Say goodbye to typing out os.path.join(a, b) over and over.

And if that’s all pathlib did, it would be a nice addition to Python — but it does a lot more!

For example, we can read the contents of a text file without having to mess with opening and closing the file:

Pro-tip: The previous examples were buggy because the opened file was never closed. This syntax avoids that bug entirely.

In fact, pathlib makes most standard file operations quick and easy:

You can even use pathlib to explicitly convert a Unix path into a Windows-formatted path:

And if you REALLY want to use backslashes in your code safely, you can declare your path as Windows-formatted and pathlib can convert it to work on the current operating system:

If you want to get fancy, you can even use pathlib to do things like resolve relative file paths, parse network share paths and generate file:// urls. Here’s an example that will open a local file in your web browser with just two lines a code:

This was just a tiny peak at pathlib. It’s a great replacement for lots of different file-related functionality that used to be scattered around different Python modules. Check it out!

What is the correct path for Python?

Setting Python Path in Unix or Linux /usr/local/bin/python is the default path of the Python directory.

How do you give a directory path in Python?

Learning Objectives.
Use the earthpy attribute et. io. HOME to find the home directory on any computer..
Use os. path. join() to create paths that will work on Windows, Mac and Linux..
Use os. path. exists() to ensure a file path exists..
Set your working directory in Python using os. chdir() ..

Which slash is used in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

Which slash is used for path?

Windows traditionally uses the backslash ( \ ) to separate directories in file paths. (For example, C:\Program Files\PuppetLabs .)