我正在尝试学习python并正在制作一个将输出脚本的程序.我想使用os.path.join,但我很困惑.根据文件,如果我说:
os.path.join('c:', 'sourcedir')
Run Code Online (Sandbox Code Playgroud)
我得到"C:sourcedir".根据文档,这是正常的,对吧?
但是当我使用copytree命令时,Python会以所需的方式输出它,例如:
import shutil
src = os.path.join('c:', 'src')
dst = os.path.join('c:', 'dst')
shutil.copytree(src, dst)
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误代码:
WindowsError: [Error 3] The system cannot find the path specified: 'C:src/*.*'
如果我换行os.path.join与os.path.normpath我得到同样的错误.
如果os.path.join不能以这种方式使用,那么我对其目的感到困惑.
根据Stack Overflow建议的页面,不应该在连接中使用斜杠 - 这是正确的,我假设?
我的 matplotlib 函数有一个非常奇怪的行为savefig。我正在做如下:
import os
# creating the output folder
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# adding the name of the figure to the created output folder
plt.savefig(output_folder + 'forecasts_scatter_%s' % model_name)
# plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))
plt.close()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "C:/Users/96171/Desktop/ministry_of_public_health/CodeUbrCorrected/bmwmlmcw.py", line 56, in <module>
lm.cross_validation(model, hyperparameters[model_name], model_name)
File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 554, in cross_validation
self.cross_validation_grid(model_used, hyperparams, model_name)
File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\testing.py", line 348, in wrapper
return fn(*args, **kwargs)
File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 1444, in cross_validation_grid
'predicted')
File …Run Code Online (Sandbox Code Playgroud) import os
xp1 = "\Documents and Settings\"
xp2 = os.getenv("USERNAME")
print xp1+xp2
Run Code Online (Sandbox Code Playgroud)
给我错误
File "1.py", line 2
xp1 = "\Documents and Settings\"
^
SyntaxError: EOL while scannning single-quoted string
Run Code Online (Sandbox Code Playgroud)
你能帮帮我吗,你看到了问题吗?
我需要解决 sys.path 提供的分隔符和 os.path.join 使用的分隔符之间的差异。
我模仿了这个 Esri 方法(共享 Python 脚本的技术)来使我的脚本可移植。它目前在 Windows 中使用,但最终将在 Linux 服务器上运行;我需要让 Python 确定适当的斜线。
他们的建议是:
# Get the pathname to this script
scriptPath = sys.path[0]
# Get the pathname to the ToolShare folder
toolSharePath = os.path.dirname(scriptPath)
# Now construct pathname to the ToolData folder
toolDataPath = os.path.join(toolSharePath, "ToolData")
print "ToolData folder: " + toolDataPath
Run Code Online (Sandbox Code Playgroud)
但是这个输出ToolData folder: C:/gis\ToolData——显然混合斜线是行不通的。
这个问题(在 windows 上混合斜线与 os.path.join)包括解决方案的基本方法:
在将其放入
os.path.join. 通过这种方式,您可以确保os.path.join不会基于可能错误的输入做出错误的决定
但是,我不确定如何确保它可以跨平台工作。如果我.replace("/","\\")在sys.path[0]结果上使用,这对 …
我在 Windows 10 上使用 python v3.6。当指定一个字符串来表示目录位置时,下面两种方法有什么区别?
folder_location = 'C:\\Users\\username\\Dropbox\\Inv'
folder_location = 'C:/Users/username/Dropbox/Inv'
Run Code Online (Sandbox Code Playgroud)
这是我刚刚发布的另一个问题的后续问题。当我使用\\而不是/.
我使用python为Autodesk Maya编写脚本.Maya是一个跨平台的软件,内部使用正斜杠.如果我在Windows上使用os.path.join操作,它可以产生如下路径:
e:/Test\\TemplatePicture.jpg
Run Code Online (Sandbox Code Playgroud)
我的想法是,只要我不使用ms-dos命令更简单的方式来加入这样的路径部分:
pathPart1 = "e:"
pathPart2 = "Test"
pathPart3 = "TemplatePicture.jpg"
path = "s%/s%/s%" % (pathPart1, pathPart2, pathPart3)
Run Code Online (Sandbox Code Playgroud)
有什么东西让它成为一个坏主意吗?
python ×6
arcpy ×1
backslash ×1
directory ×1
matplotlib ×1
maya ×1
path ×1
portability ×1
python-3.x ×1
syntax-error ×1
sys ×1
windows ×1