在python os.system中浏览不同的驱动器号

时间:2023-01-26 21:38:42

I am having a problem with a bit of code on one windows machine but not all windows machines. i have the following code:

我在一台Windows机器上遇到了一些问题,但并不是所有的Windows机器都有问题。我有以下代码:

path = "F:/dir/"
os.system(path[0:2] + " && cd " + path + " && git init")

On all but one of my windows systems it runs fine but on a windows 2003 server it gives a "directory not found" error but if i run the same command flat from the command prompt than it works.

除了我的一个Windows系统之外,它运行正常,但是在Windows 2003服务器上它会出现“找不到目录”错误但是如果我从命令提示符运行相同的命令而不是它的工作。

I'm sorry if my question comes off as vague but I'm totally stumped

如果我的问题模糊不清,我很抱歉,但我完全被难倒了

1 个解决方案

#1


os.path contains many usefull path manipulation functions. Probably just handling the path cleanly will resolve your problem.

os.path包含许多有用的路径操作函数。可能只是干净地处理路径将解决您的问题。

>>> import os
>>>
>>>
>>> path = "F:/dir/"
>>>
>>> clean_path = os.path.normpath(path)
>>> clean_path
'F:\\dir'
>>> drive, directory = os.path.splitdrive(clean_path)
>>> drive
'F:'
>>> directory
'\\dir'

Also, you might want to look into using the subprocess module, it gives you more control over processes.

此外,您可能希望查看使用子进程模块,它可以让您更好地控制进程。

Replacing Older Functions with the subprocess Module

用子进程模块替换旧函数

#1


os.path contains many usefull path manipulation functions. Probably just handling the path cleanly will resolve your problem.

os.path包含许多有用的路径操作函数。可能只是干净地处理路径将解决您的问题。

>>> import os
>>>
>>>
>>> path = "F:/dir/"
>>>
>>> clean_path = os.path.normpath(path)
>>> clean_path
'F:\\dir'
>>> drive, directory = os.path.splitdrive(clean_path)
>>> drive
'F:'
>>> directory
'\\dir'

Also, you might want to look into using the subprocess module, it gives you more control over processes.

此外,您可能希望查看使用子进程模块,它可以让您更好地控制进程。

Replacing Older Functions with the subprocess Module

用子进程模块替换旧函数