问题:如何在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

还应提及,因为它将Windows上的/路径分隔符转换为\分隔符。它还折叠冗余uplevel引用…即,A/BA/foo/../BA/./B一切变得A/B。如果您使用的是Windows,那么所有这些都将变为A\B

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

做一个import os然后使用os.sep

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)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。