简体   繁体   中英

Windows path with double backslash in Python

This seems to be very basic question but still I am confused. I have a windows path containing backslash, which to escape its special meaning I have used \\ . While I use print function get the path, gives me the actual return:

>>> print("C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51\\Resources")
C:\Users\2.0Dev\8\F000B101\POD280-51\Resources

however, when the same is passed as an argument to two different functions in python, the behavior is different:

>>> rsrc_dir="C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51\\Resources"
>>> os.path.isdir(rsrc_dir)
>>> False
>>> os.mkdir('C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51\\Resources')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: [Error 3] The system cannot find the path specified:'C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51\\Resources'

Can someone please explain how the two functions interpret the same parameter. Also, how to return the formatted string same as the print function.

Much Thanks.

os.mkdir does not create intermediate catalogs, so this:

os.mkdir('C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51\\Resources')

would fail if not

os.path.exists('C:\\Users\\2.0Dev\\8\\F000B101\\POD280-51')

use os.makedirs if you want recursive directory creation. Note that you might use os.path.join which will use separator appriopiate to system at which it runs, in your case usage would be:

rsrc_dir=os.path.join("C:\\","Users","2.0Dev","8","F000B101","POD280-51","Resources")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM