Windows path in Python
What is the best way to represent a Windows directory, for example "C:meshesas"? I have been trying to modify a script but it never works because I can't seem to get the directory right, I assume because of the '' acting as escape character?
python path string-literals
add a comment |
What is the best way to represent a Windows directory, for example "C:meshesas"? I have been trying to modify a script but it never works because I can't seem to get the directory right, I assume because of the '' acting as escape character?
python path string-literals
add a comment |
What is the best way to represent a Windows directory, for example "C:meshesas"? I have been trying to modify a script but it never works because I can't seem to get the directory right, I assume because of the '' acting as escape character?
python path string-literals
What is the best way to represent a Windows directory, for example "C:meshesas"? I have been trying to modify a script but it never works because I can't seem to get the directory right, I assume because of the '' acting as escape character?
python path string-literals
python path string-literals
edited Mar 31 '18 at 11:46
vaultah
27.9k973101
27.9k973101
asked Jun 1 '10 at 22:29
Gareth
8812820
8812820
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
you can use always:
'C:/mydir'
this works both in linux and windows.
Other posibility is
'C:\mydir'
if you have problems with some names you can also try raw string literals:
r'C:mydir'
however best practice is to use the os.path module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent to the above:
pathlib.Path(mydir, myfile)
or
pathlib.Path(mydir) / myfile
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
add a comment |
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:meshesas"
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put"C:"as the first entry? Does that mess up some of cleanliness of usingjoin?
– Jack O'Connor
Feb 21 '14 at 0:53
1
@JackO'Connor that's what I do. You certainly do not want to put"C:"in the middle of the file name. Besides, you can useos.path.normpathbefore or after a join, to make sure the path gets printed nicely.
– Agostino
Apr 7 '15 at 18:03
add a comment |
Yes, in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence a, which is collapsed into one character that is ASCII Bell:
>>> 'a'
'x07'
>>> len('a')
1
>>> 'C:meshesas'
'C:\meshesx07s'
>>> print('C:meshesas')
C:meshess
Other common escape sequences include t (tab), n (line feed), r (carriage return):
>>> list('C:test')
['C', ':', 't', 'e', 's', 't']
>>> list('C:nest')
['C', ':', 'n', 'e', 's', 't']
>>> list('C:rest')
['C', ':', 'r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
rorR:
>>> r'C:meshesas'
'C:\meshes\as'
>>> print(r'C:meshesas')
C:meshesas
Python on Windows should handle forward slashes, too.
You could use
os.path.join...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\meshes\as'
... or the newer
pathlibmodule
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2953834%2fwindows-path-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can use always:
'C:/mydir'
this works both in linux and windows.
Other posibility is
'C:\mydir'
if you have problems with some names you can also try raw string literals:
r'C:mydir'
however best practice is to use the os.path module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent to the above:
pathlib.Path(mydir, myfile)
or
pathlib.Path(mydir) / myfile
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
add a comment |
you can use always:
'C:/mydir'
this works both in linux and windows.
Other posibility is
'C:\mydir'
if you have problems with some names you can also try raw string literals:
r'C:mydir'
however best practice is to use the os.path module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent to the above:
pathlib.Path(mydir, myfile)
or
pathlib.Path(mydir) / myfile
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
add a comment |
you can use always:
'C:/mydir'
this works both in linux and windows.
Other posibility is
'C:\mydir'
if you have problems with some names you can also try raw string literals:
r'C:mydir'
however best practice is to use the os.path module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent to the above:
pathlib.Path(mydir, myfile)
or
pathlib.Path(mydir) / myfile
you can use always:
'C:/mydir'
this works both in linux and windows.
Other posibility is
'C:\mydir'
if you have problems with some names you can also try raw string literals:
r'C:mydir'
however best practice is to use the os.path module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent to the above:
pathlib.Path(mydir, myfile)
or
pathlib.Path(mydir) / myfile
edited Dec 28 '18 at 5:14
gilbertbw
3292520
3292520
answered Jun 1 '10 at 22:30
joaquin
53.9k21123138
53.9k21123138
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
add a comment |
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
1
1
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
Thanks guys, '/' worked fine, but the other hints are appreciated.
– Gareth
Jun 1 '10 at 22:35
2
2
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify.
– joaquin
Jun 1 '10 at 22:48
10
10
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
The only thing to be careful with on raw strings is that they can't end with
– Douglas Leeder
Jun 29 '10 at 16:15
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
You can use os.path.join() to remove the need to end paths with .
– Will Ediger
Aug 5 '14 at 14:53
1
1
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes
– peterb
Aug 21 '16 at 5:07
add a comment |
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:meshesas"
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put"C:"as the first entry? Does that mess up some of cleanliness of usingjoin?
– Jack O'Connor
Feb 21 '14 at 0:53
1
@JackO'Connor that's what I do. You certainly do not want to put"C:"in the middle of the file name. Besides, you can useos.path.normpathbefore or after a join, to make sure the path gets printed nicely.
– Agostino
Apr 7 '15 at 18:03
add a comment |
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:meshesas"
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put"C:"as the first entry? Does that mess up some of cleanliness of usingjoin?
– Jack O'Connor
Feb 21 '14 at 0:53
1
@JackO'Connor that's what I do. You certainly do not want to put"C:"in the middle of the file name. Besides, you can useos.path.normpathbefore or after a join, to make sure the path gets printed nicely.
– Agostino
Apr 7 '15 at 18:03
add a comment |
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:meshesas"
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:meshesas"
answered Jun 1 '10 at 22:30
S.Lott
315k66437716
315k66437716
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put"C:"as the first entry? Does that mess up some of cleanliness of usingjoin?
– Jack O'Connor
Feb 21 '14 at 0:53
1
@JackO'Connor that's what I do. You certainly do not want to put"C:"in the middle of the file name. Besides, you can useos.path.normpathbefore or after a join, to make sure the path gets printed nicely.
– Agostino
Apr 7 '15 at 18:03
add a comment |
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put"C:"as the first entry? Does that mess up some of cleanliness of usingjoin?
– Jack O'Connor
Feb 21 '14 at 0:53
1
@JackO'Connor that's what I do. You certainly do not want to put"C:"in the middle of the file name. Besides, you can useos.path.normpathbefore or after a join, to make sure the path gets printed nicely.
– Agostino
Apr 7 '15 at 18:03
9
9
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\as' on Windows.)
– dash-tom-bang
Jun 1 '10 at 23:04
@dash-tom-bang's comment is really important. Is the right thing to do to put
"C:" as the first entry? Does that mess up some of cleanliness of using join?– Jack O'Connor
Feb 21 '14 at 0:53
@dash-tom-bang's comment is really important. Is the right thing to do to put
"C:" as the first entry? Does that mess up some of cleanliness of using join?– Jack O'Connor
Feb 21 '14 at 0:53
1
1
@JackO'Connor that's what I do. You certainly do not want to put
"C:" in the middle of the file name. Besides, you can use os.path.normpath before or after a join, to make sure the path gets printed nicely.– Agostino
Apr 7 '15 at 18:03
@JackO'Connor that's what I do. You certainly do not want to put
"C:" in the middle of the file name. Besides, you can use os.path.normpath before or after a join, to make sure the path gets printed nicely.– Agostino
Apr 7 '15 at 18:03
add a comment |
Yes, in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence a, which is collapsed into one character that is ASCII Bell:
>>> 'a'
'x07'
>>> len('a')
1
>>> 'C:meshesas'
'C:\meshesx07s'
>>> print('C:meshesas')
C:meshess
Other common escape sequences include t (tab), n (line feed), r (carriage return):
>>> list('C:test')
['C', ':', 't', 'e', 's', 't']
>>> list('C:nest')
['C', ':', 'n', 'e', 's', 't']
>>> list('C:rest')
['C', ':', 'r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
rorR:
>>> r'C:meshesas'
'C:\meshes\as'
>>> print(r'C:meshesas')
C:meshesas
Python on Windows should handle forward slashes, too.
You could use
os.path.join...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\meshes\as'
... or the newer
pathlibmodule
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
add a comment |
Yes, in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence a, which is collapsed into one character that is ASCII Bell:
>>> 'a'
'x07'
>>> len('a')
1
>>> 'C:meshesas'
'C:\meshesx07s'
>>> print('C:meshesas')
C:meshess
Other common escape sequences include t (tab), n (line feed), r (carriage return):
>>> list('C:test')
['C', ':', 't', 'e', 's', 't']
>>> list('C:nest')
['C', ':', 'n', 'e', 's', 't']
>>> list('C:rest')
['C', ':', 'r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
rorR:
>>> r'C:meshesas'
'C:\meshes\as'
>>> print(r'C:meshesas')
C:meshesas
Python on Windows should handle forward slashes, too.
You could use
os.path.join...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\meshes\as'
... or the newer
pathlibmodule
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
add a comment |
Yes, in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence a, which is collapsed into one character that is ASCII Bell:
>>> 'a'
'x07'
>>> len('a')
1
>>> 'C:meshesas'
'C:\meshesx07s'
>>> print('C:meshesas')
C:meshess
Other common escape sequences include t (tab), n (line feed), r (carriage return):
>>> list('C:test')
['C', ':', 't', 'e', 's', 't']
>>> list('C:nest')
['C', ':', 'n', 'e', 's', 't']
>>> list('C:rest')
['C', ':', 'r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
rorR:
>>> r'C:meshesas'
'C:\meshes\as'
>>> print(r'C:meshesas')
C:meshesas
Python on Windows should handle forward slashes, too.
You could use
os.path.join...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\meshes\as'
... or the newer
pathlibmodule
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
Yes, in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence a, which is collapsed into one character that is ASCII Bell:
>>> 'a'
'x07'
>>> len('a')
1
>>> 'C:meshesas'
'C:\meshesx07s'
>>> print('C:meshesas')
C:meshess
Other common escape sequences include t (tab), n (line feed), r (carriage return):
>>> list('C:test')
['C', ':', 't', 'e', 's', 't']
>>> list('C:nest')
['C', ':', 'n', 'e', 's', 't']
>>> list('C:rest')
['C', ':', 'r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
rorR:
>>> r'C:meshesas'
'C:\meshes\as'
>>> print(r'C:meshesas')
C:meshesas
Python on Windows should handle forward slashes, too.
You could use
os.path.join...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\meshes\as'
... or the newer
pathlibmodule
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
edited Mar 31 '18 at 13:49
answered Mar 31 '18 at 12:31
vaultah
27.9k973101
27.9k973101
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2953834%2fwindows-path-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown