简体   繁体   English

python os.path.join用于冻结的exe

[英]python os.path.join for a frozen exe

I am working on code that uses os.walk to search through a relative path. 我正在使用os.walk来搜索相对路径的代码。 When I run it as a python script I have no problems but after converting it to an exe it can't seem to find the relative path. 当我将其作为python脚本运行时,我没有问题,但是将其转换为exe后似乎找不到相对路径。 The current path prints fine Below is the current solution that i have been working on. 当前路径可以正常打印以下是我一直在努力的当前解决方案。

 if getattr(sys, 'frozen', False): currentPath = os.path.dirname(sys.executable) relativePath = os.path.join(currentPath,'/../../folder') else: currentPath = inspect.stack()[0][1] relativePath = os.path.join(currentPath,'/../../folder') 

for root, dirs, files in os.walk(relativePath):

When hardcoding the relativePath the exe works. 硬编码relativePath时,exe可以工作。

relativePath = "D:/location/../../folder" relativePath =“ D:/位置/../../文件夹”

Is there something tricky with joining when converting to an exe that I'm missing? 转换为我所缺少的exe时,加入时有些棘手的事情吗?

I'm not sure but the cause could be that you are mixing backslashes and forward slashes in the path. 我不确定,但是原因可能是您在路径中混用了反斜杠和正斜杠。

Try changing the code that creates relativePath to the following: 尝试将创建relativePath的代码更改为以下代码:

relativePath = os.path.join(currentPath, '..', '..', 'folder')

This should ensure that you are definitely using the correct path separator. 这应该确保您肯定使用了正确的路径分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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