问题:如何在Linux和Windows中的Python中使用“ /”(目录分隔符)?
我已经在python中编写了一个代码,该代码使用/在文件夹中创建特定文件,如果我想在Windows中使用该代码将无法正常工作,有没有一种方法可以在Windows和Linux中使用该代码。
在python中,我使用以下代码:
pathfile=os.path.dirname(templateFile)
rootTree.write(''+pathfile+'/output/log.txt')
当我在Windows计算机中使用我的代码时,我的代码将无法工作。
在Linux和Windows中如何使用“ /”(目录分隔符)?
I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use the code in Windows and Linux.
In python I am using this code:
pathfile=os.path.dirname(templateFile)
rootTree.write(''+pathfile+'/output/log.txt')
When I will use my code in suppose windows machine my code will not work.
How do I use “/” (directory separator) in both Linux and Windows?
回答 0
使用os.path.join()
。范例:os.path.join(pathfile,"output","log.txt")
。
在您的代码中将是: rootTree.write(os.path.join(pathfile,"output","log.txt"))
Use os.path.join()
.
Example: os.path.join(pathfile,"output","log.txt")
.
In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))
回答 1
用:
import os
print os.sep
查看分隔符在当前操作系统上的外观。
在您的代码中,您可以使用:
import os
path = os.path.join('folder_name', 'file_name')
Use:
import os
print os.sep
to see how separator looks on a current OS.
In your code you can use:
import os
path = os.path.join('folder_name', 'file_name')
回答 2
您可以使用os.sep:
>>> import os
>>> os.sep
'/'
You can use os.sep:
>>> import os
>>> os.sep
'/'
回答 3
os.path.normpath(pathname)
还应提及,因为它将Windows上的/
路径分隔符转换为\
分隔符。它还折叠冗余uplevel引用…即,A/B
与A/foo/../B
和A/./B
一切变得A/B
。如果您使用的是Windows,那么所有这些都将变为A\B
。
os.path.normpath(pathname)
should also be mentioned as it converts /
path separators into \
separators on Windows. It also collapses redundant uplevel references… i.e., A/B
and A/foo/../B
and A/./B
all become A/B
. And if you are Windows, these all become A\B
.
回答 4
如果您有幸能够运行Python 3.4+,则可以使用pathlib
:
from pathlib import Path
path = Path(dir, subdir, filename) # returns a path of the system's path flavour
或者,等效地,
path = Path(dir) / subdir / filename
If you are fortunate enough to be running Python 3.4+, you can use pathlib
:
from pathlib import Path
path = Path(dir, subdir, filename) # returns a path of the system's path flavour
or, equivalently,
path = Path(dir) / subdir / filename
回答 5
Some useful links that will help you:
回答 6
Do a import os
and then use os.sep
回答 7
您可以使用“ os.sep ”
import os
pathfile=os.path.dirname(templateFile)
directory = str(pathfile)+os.sep+'output'+os.sep+'log.txt'
rootTree.write(directory)
You can use “os.sep “
import os
pathfile=os.path.dirname(templateFile)
directory = str(pathfile)+os.sep+'output'+os.sep+'log.txt'
rootTree.write(directory)
回答 8
不要自行建立目录和文件名,请使用python随附的库。
在这种情况下,相关的是os.path。特别是join,它从目录和文件名或目录创建一个新的路径名,然后从完整路径中获取文件名。
你的例子是
pathfile=os.path.dirname(templateFile)
p = os.path.join(pathfile, 'output')
p = os.path.join( p, 'log.txt')
rootTree.write(p)
Don’t build directory and file names your self, use python’s included libraries.
In this case the relevant one is os.path. Especially join which creates a new pathname from a directory and a file name or directory and split that gets the filename from a full path.
Your example would be
pathfile=os.path.dirname(templateFile)
p = os.path.join(pathfile, 'output')
p = os.path.join( p, 'log.txt')
rootTree.write(p)