Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is os.path.join necessary?

Currently I use os.path.join almost always in my django project for cross OS support; the only places where I don't currently use it are for template names and for URLs. So in situations where I want the path '/path/to/some/file.ext' I use os.path.join('path', 'to', 'some', 'file.ext').

However I just tested my project on windows to see whether that worked fine / was necessary and it seems windows will happily accept '/' or '\\' (or '\' when working outside of python), and as all UNIX systems all use '/' it seems like there is no reason to ever use '\\', in which case is it necessary to use os.path.join anywhere?

Is there a situation in which adding a '/' or using posixpath will cause problems on certain operating systems (not including XP or below as they are no longer officially supported)? If not I think I will just use posixpath or adding a '/' for joining variables with other variables or variables with strings and not separate out string paths (so leave it as '/path/to/some/file.ext') unless there is another reason for me to not do that other than it breaking things.

To avoid this being potentially closed as primarily-opinion based I would like to clarify that my specific question is whether not using os.path.join will ever cause a python program to not work as intended on a supported operating system.

like image 287
semicolon Avatar asked Jun 06 '14 01:06

semicolon


People also ask

Why do we need os path join?

Using os. path. join makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically.

Does os path join work for Windows?

This works on any platform where users have a home directory, including Linux, Mac OS X, and Windows. The returned path does not have a trailing slash, but the os. path. join() function doesn't mind.

What is os path used for?

The os. path module is a very extensively used module that is handy when processing files from different places in the system. It is used for different purposes such as for merging, normalizing and retrieving path names in python .

Is Pathlib better than os?

Summary. In this article, I have introduced another Python built-in library, the Pathlib. It is considered to be more advanced, convenient and provides more stunning features than the OS library. Of course, we still need to know how to use the OS library as it is one of the most powerful and basic libraries in Python.


2 Answers

The Microsoft Windows API doesn't care whether you use / or \, so it's normally fine to use either as a separator on Windows. However, command line ("DOS box" - command.com or cmd.exe) commands generally require \ in paths (/ is used to flag command options in these native Windows shells). So, for example, if you build a command line in Python and fire off a shell to execute the command, you'll generally need to use the \ separator on Windows.

One other case is covered in Lib/macpath.py: there sep is set to : (a colon), catering to older Macintosh systems. I believe that's the only system Python has ever run on that doesn't accept / as a separator.

EDIT: see here for a long account of Windows naming rules. Don't blame me ;-)

like image 168
Tim Peters Avatar answered Oct 17 '22 08:10

Tim Peters


If you are presenting a filename to a user for any reason, it's better if that filename follows the usual OS conventions.

Windows has been able to use the / for path separators for as long as there have been paths - this was a DOS feature.

like image 29
Mark Ransom Avatar answered Oct 17 '22 07:10

Mark Ransom