I write some simple Python script and I want to replace all characters / with \ in text variable. I have problem with character \, because it is escape character. When I use replace() method:
unix_path='/path/to/some/directory'
unix_path.replace('/','\\')
then it returns following string: \\path\\to\\some\\directory. Of course, I can't use: unix_path.replace('/','\'), because \ is escape character.
When I use regular expression:
import re
unix_path='/path/to/some/directory'
re.sub('/', r'\\', unix_path)
then it has same results: \\path\\to\\some\\directory. I would like to get this result: \path\to\some\directory.
Note: I aware of os.path, but I did not find any feasible method in this module.
You missed something: it is shown as \\ by the Python interpreter, but your result is correct: '\\'is just how Python represents the character \ in a normal string. That's strictly equivalent to \ in a raw string, e.g. 'some\\path is same as r'some\path'.
And also: Python on windows knows very well how to use / in paths.
You can use the following trick though, if you want your dislpay to be OS-dependant:
In [0]: os.path.abspath('c:/some/path')
Out[0]: 'c:\\some\\path'
You don't need a regex for this:
>>> unix_path='/path/to/some/directory'
>>> unix_path.replace('/', '\\')
'\\path\\to\\some\\directory'
>>> print(_)
\path\to\some\directory
And, more than likely, you should be using something in os.path instead of messing with this sort of thing manually.
This worked for me:
unix_path= '/path/to/some/directory'
print(unix_path.replace('/','\\'))
result:
\path\to\some\directory
Related
I am using replace string method in Python and I am finding something that I cannot understand.
Changing the way that a folder is written in python to windows notation, I find that replace method will change this double / for a double \ instead of just one \ as intended.
folder_im_wdows = folder_im_wdows.replace("//","\\")
But the most impressive, is that when I try a workaround doing the next
folder_im_wdows = folder_im_wdows.replace("//",chr(92))
Python does the same...
The original variable is: //xxxxx//xxxx//xxxx//xxxx//xxx//xxxxx
And I want to get -> \xxx\x\x\x
What's happening with replace method?
This is because python's CLI escapes backslashes.
Example from python's CLI:
>>> str = "abc//def//fgh"
>>> str.replace("//", "\\")
'abc\\def\\fgh'
>>> print(str.replace("//", "\\"))
abc\def\fgh
>>>
Also, you should need to use \\ and not only \, because you need to escape the backslash character, well, I do.
Use os.path for working with path names:
import os
os.path.normpath('C:/Users/Bob/My Documents')
os.path.abspath would do the job too (it uses os.path.normpath)
Note: requires host to be windows, if that's not the case you can use ntpath.normpath directly
https://docs.python.org/library/os.path.html#os.path.normpath
Avoid regexes, replaces and all that. You're going to get it wrong in some subtle way.
I am trying to create a string in Python using the format method, but one of the arguments is a regular expression. I tried:
>>> 'foo{}bar'.format('[\s]+')
'foo[\\s]+bar'
Because of the escaping, I can't use the result as a re.search pattern. Is there a way not to escape it?
Thanks.
In fact, you don't need to do anything, because the result is what you expect. foo[\\s]+bar is the representation, but not the real value, which is foo[\s]+bar. Try this:
>> print 'foo{}bar'.format('[\s]+')
# and you will get
>> 'foo[\s]+bar'
Is there a python equivalent to echo -e?
In other words, is there a built-in function to convert r"\x50\x79\x74\x68\x6f\x6e" to "Python" in Python?
Edit
I added the 'r' prefix, to make sure everyone understands that I do not want the python interpreter to convert this. Rather, I want to convert that 24-character string to a 6-character one.
The correct way to do this, which I just found is
>>> a = r"\x50\x79\x74\x68\x6f\x6e"
>>> print a
\x50\x79\x74\x68\x6f\x6e
>>> a.decode('string_escape')
'Python'
Make sure you are escaping the backslashes (or using the raw 'r' prefix) when testing this!
References:
http://docs.python.org/library/stdtypes.html#str.decode
http://docs.python.org/library/codecs.html#standard-encodings
No conversion is necessary. They are already the same string
>>> "\x50\x79\x74\x68\x6f\x6e" == "Python"
True
If you actually have a different string "\\x50\\x79\\x74\\x68\\x6f\\x6e" which actually contains backslashes ("\x50\x79\x74\x68\x6f\x6e" does not contain any backslashes), then you would do
>>> s
'\\x50\\x79\\x74\\x68\\x6f\\x6e'
>>> s.decode('string-escape')
'Python'
Hi
I have read articles related converting backward to forward slashes.
But sol was to use raw string.
But Problem in my case is :
I will get file path dynamically to a variable
var='C:\dummy_folder\a.txt'
In this case i need to convert it to Forward slashes.
But due to '\a',i am not able to convert to forward slashes
How to i convert it? OR How should i change this string to raw string so that i can change it to forward slash
Don't do this. Just use os.path and let it handle everything. You should not explicitly set the forward or backward slashes.
>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\\', '/')
'C:/dummy_folder/a.txt'
But again, don't. Just use os.path and be happy!
There is also os.path.normpath(), which converts backslashes and slashes depending on the local OS. Please see here for detailed usage info. You would use it this way:
>>> string = r'C:/dummy_folder/a.txt'
>>> os.path.normpath(string)
'C:\dummy_folder\a.txt'
Handling paths as a mere string could put you into troubles.; even more if the path you are handling is an user input or may vary in unpredictable ways.
Different OS have different way to express the path of a given file, and every modern programming language has own methods to handle paths and file system references. Surely Python and Ruby have it:
Python: os.path
Ruby: File and FileUtils
If you really need to handle strings:
Python: string.replace
Ruby : string.gsub
Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.
To replace backslashes with forward slashes:
# Python:
string = r'C:\dummy_folder\a.txt'
string = string.replace('\\', '/')
# Ruby:
string = 'C:\\dummy_folder\\a.txt'
string = string.gsub('\\', '/')
>>> 'C:\\dummy_folder\\a.txt'.replace('\\', '/')
'C:/dummy_folder/a.txt'
In a string literal, you need to escape the \ character.
There are times that I automagically create small shell scripts from Python, and I want to make sure that the filename arguments do not contain non-escaped special characters. I've rolled my own solution, that I will provide as an answer, but I am almost certain I've seen such a function lost somewhere in the standard library. By “lost” I mean I didn't find it in an obvious module like shlex, cmd or subprocess.
Do you know of such a function in the stdlib? If yes, where is it?
Even a negative (but definite and correct :) answer will be accepted.
pipes.quote():
>>> from pipes import quote
>>> quote("""some'horrible"string\with lots of junk!$$!""")
'"some\'horrible\\"string\\\\with lots of junk!\\$\\$!"'
Although note that it's arguably got a bug where a zero-length arg will return nothing:
>>> quote("")
''
Probably it would be better if it returned '""'.
The function I use is:
def quote_filename(filename):
return '"%s"' % (
filename
.replace('\\', '\\\\')
.replace('"', '\"')
.replace('$', '\$')
.replace('`', '\`')
)
that is: I always enclose the filename in double quotes, and then quote the only characters special inside double quotes.