Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have few python scripts which were written by someone else in Windows. These scripts use double backslash as path separator but when run in Ubuntu they don't work because of this. I can change all \ to / and scripts will work but there are lot of scripts and its a lot of effort to change each \ to /

So my question is: Is it possible to add \ as path separator in Ubuntu or only in Bash so that I dont have to modify these scripts?

Example code:

# Append Common directory to the Python search path
sys.path.append('..\\..\\..' + os.sep + 'Common')
share|improve this question

2 Answers 2

In Linux, filenames can contain any characters except / (since it's the path separator) and the NUL byte (the string terminator, \0). That means \ is a valid character in a filename, as well as newlines, tabs, terminal escape sequences, unprintable characters... so no, you can't temporarily use \ as a path separator.

Windows is not as liberal, and has a larger set of characters not allowed in filenames, and / is one of those characters not allowed. For this reason, python will also consider / a path separator on Windows, so changing those \\ to / should make the script work on both Windows and Ubuntu. That said, the proper way to join paths in python is to use the os.path.join function.

I recommend going through the scripts and manually fix all the non-portable code.

share|improve this answer

I cannot answer you question, but it is very easy to change \ to / in all scripts like this:

find <root_of_scripts> -name "*.py" -exec sed -i 's/\\\\/\//g' {} \;

(Just try it on a copy of your files first!)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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