0
dir = "C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf"

print dir

using the code above, i get this... i know that \t is tab

C:\Users\Geraldes\Desktop\media  (espacamento)  este\ASMS_TapeA01A1691329.mxf

but, to fix this i do:

dir1 = dir.replace("\\", "\\\\")

print "dir:",dir1

and i get

C:\\Users\\Geraldes\\Desktop\\media (espacamento)  este\\ASMS_TapeA01A1691329.mxf

how can i fix this problem?

5 Answers 5

5

escape your original string's backslashes, or use raw strings.

That is,

dir = "C:\\Users\\Geraldes\\Desktop\\media\\teste\\ASMS_TapeA01A1691329.mxf"

or

dir = r"C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf"

BUT: be careful with the second choice, because raw strings were not invented for windows paths - they were put there for regular expressions. Thus, one day you will find that you want to put a backslash at the end of the string, like this:

dir = "C:\Users\Geraldes\Desktop\media\teste\"

It won't work. This is discussed in greater depth here.

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

4 Comments

thanks guys, i was having a bad time with kinda simple thing ;)
You can write it as: r"C:\whatever\whatever\\". You'll get two backslashes at the end, but that doesn't matter. The path still works fine that way!
Some things will choke on trailing slashes and double slashes. Not everything parses paths correctly.
True. I should say that Windows is fine with having an empty segment in a pathname, but who knows what expectations other code might have.
4

Nobody said but, in addition to expanded backlashes and raw strings, you can also use forward slashes

>>> dir = "C:/Users/Geraldes/Desktop/media/teste/ASMS_TapeA01A1691329.mxf"
>>> dir
'C:/Users/Geraldes/Desktop/media/teste/ASMS_TapeA01A1691329.mxf'
>>> 

In fact, forward slashes are accepted by Windows (Check this for more info)

Microsoft Windows [Versión 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Reservados todos los derechos.

C:\>cd Users/joaquin

C:\Users\joaquin>

In any case the safest method is to use os.path.join to build your paths in an OS agnostic way.

>>> import os
>>> os.path.join('C:\Users', 'Geraldes', 'Desktop', 'media', 'teste', 'ASMS.mxf')
'C:\\Users\\Geraldes\\Desktop\\media\\teste\\ASMS.mxf'
>>> 

4 Comments

Forward slashes work with most things, but not by a long shot with all. It's not safe to assume that any given thing will work with forward slashes - far too many don't.
@Chris Morgan, right, there is a discussion on the link I gave. Still I never found any problem for the case discussed (for pathname strings in windows). It would be interesting if you could give an example showing the potential problems of slashes.
I would also point out that you can use os.path.normpath (discussed in the article I linked to in my answer) to avoid any potential trouble with slashes in paths
@joaquin: I have come across some things that don't work properly with forward slashes, but I can't think of any of them offhand. The main nuisance that I've found with it is that path completion in Command Prompt doesn't work if you use forward slashes.
3

Define a "raw" string:

dir = r"C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf"

1 Comment

great, never knew about this way of doing it; goodbye double-slashes !
3

You can use raw formatting:

dir = r"C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf"

This will print as:

>>> print(dir)
C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf

Comments

0

A less obvious and less efficient way is to do the following:

import os
os.chdir()#change to each directory separately
print os.getcwd()

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.