281

I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use the code in Windows and Linux.

In python I am using this code:

pathfile=os.path.dirname(templateFile)
rootTree.write(''+pathfile+'/output/log.txt')

When I will use my code in suppose windows machine my code will not work.

How do I use "/" (directory separator) in both Linux and Windows?

12
  • 1
    You can define it in the beginning depending on Win/*nix and then work with the variable. Commented Apr 15, 2013 at 8:43
  • 14
    In Windows you can use either \ or / as a directory separator. Commented Apr 15, 2013 at 8:43
  • 16
    Windows supports / in directory paths. What specific problem are you having? Post some code that illustrates the problem. Commented Apr 15, 2013 at 8:44
  • 1
    @MichaelGeary: Only sometimes. Sometimes it doesn't. (Only saying it from experience -- I don't know exactly in which situations it fails and in which it doesn't.) Commented Apr 15, 2013 at 8:50
  • 1
    @Mehrdad: Do you know an example for which the Win32 API doesn't accept '/'? (not counting cmd.exe and other programs) Commented Apr 15, 2013 at 8:54

11 Answers 11

349

Use os.path.join(). Example: os.path.join(pathfile,"output","log.txt").

In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))

Sign up to request clarification or add additional context in comments.

3 Comments

os.path.join uses a more complex logic to match several relative path components together. When you just want to chain them, os.sep.join is the right choice.
So os.sep is the delimiter string, no thanks for the confusion.
Yes, Richard, 'os.sep' is indeed the delimiter string for the specific OS where Python is running
182

Use:

import os
print os.sep

to see how separator looks on a current OS.
In your code you can use:

import os
path = os.path.join('folder_name', 'file_name')

Comments

103

You can use os.sep:

>>> import os
>>> os.sep
'/'

Comments

79

os.path.normpath(pathname) should also be mentioned as it converts / path separators into \ separators on Windows. It also collapses redundant uplevel references... i.e., A/B and A/foo/../B and A/./B all become A/B. And if you are Windows, these all become A\B.

4 Comments

This is IMO the best answer to the question as it was phrased, "how to use “/” (directory separator) in both Linux and Windows". And it's also eminently useful -- I'd much rather do os.path.normpath('a/b/c/d/file.ext') than os.path.join('a','b','c','d','file.ext') when I need to specify a long path.
I also found this answer to be very helpful. I was looking for a method for generating paths with a consistent separator. The famous os.path.join just joins anything provided. e.g. join("a/b", "c\d") gives a/b\c\d (on windows). But i can get the expected result with the proper combination of join and normpath, e.g. a\b\c\d (on windows)
I use this import: from os.path import normpath as normpath # Given a Windows or Linux path, fixes path separators to match the current OS. then I can use something like normpath("a/b/c/d/e/f") which seems cleaner than any alternative.
I know this is an old answer, but bro, your answer helped me get work done in the deadline! If a program was written for Linux and needs to be ported to Windows, this is the perfect option among all the ones here.
47

In Python 3.4+ you can use pathlib, which allows your code to be platform-independent:

from pathlib import Path

path = Path(dir, subdir, filename)  # returns a path of the system's path flavour

or, equivalently,

path = Path(dir) / subdir / filename

When you convert a path back to a string, it'll use the native path separators (e.g. backslashes on Windows).

1 Comment

It's 2020, so this should now be the accepted answer. The Python devs added it to overcome problems in os, and it makes everything easier. Ex path.read_text(encoding="utf8"), path.read_bytes(), path.resolve(), path.unlink(), ...
21

Some useful links that will help you:

3 Comments

pathsep? Generally useful, but not here, IMO.
@glglgl Indeed. I looked for sep but couldn't resist myself to post this too (I assumed the OP will found it useful for future work) :)
@Maroun os.pathsep is a semicolon: ;. Recommend editing to remove.
13

Do a import os and then use os.sep

Comments

13

You can use "os.sep "

import os
pathfile=os.path.dirname(templateFile)
directory = str(pathfile)+os.sep+'output'+os.sep+'log.txt'
rootTree.write(directory)

1 Comment

It can also be written as os.path.sep.
5

Don't build directory and file names your self, use python's included libraries.

In this case the relevant one is os.path. Especially join which creates a new pathname from a directory and a file name or directory and split that gets the filename from a full path.

Your example would be

pathfile=os.path.dirname(templateFile)
p = os.path.join(pathfile, 'output')
p = os.path.join( p, 'log.txt')
rootTree.write(p)

Comments

5

If someone is looking for something like this:

He/she wants to know the parent directory and then go to the sub-folders and maybe than to a specific file. If so, I use the following approach.

  1. I am using python 3.9 as of now. So in that version, we have the os module for handling such tasks. So, for getting the parent directory:
parent_dir = os.path.pardir
  1. It's a good coding practice to not hardcode the file path separators (/ or \). Instead, use the operating system dependant mechanism provided by the above-mentioned os module. It makes your code very much reusable for other purposes/people. It goes like this (just an example) :

path = os.path.pardir + os.sep + 'utils' + os.sep + 'properties.ini'

print(f'The path to my global properties file is :: {path}')

Output:

..\utils\properties.ini

You can surely look at the whole documentation here : https://docs.python.org/3/library/os.html

Comments

1

I use pathlib for most things, so I like: pathlib.os.sep.

Usually pathlib is the better choice if you don't need os!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.