在python os.system中通过不同的驱动器字母导航 [英] navigating through different drive letters in python os.system
问题描述
我在一个Windows机器上有一个代码的问题,但不是所有的Windows机器。我有以下代码:
I am having a problem with a bit of code on one windows machine but not all windows machines. i have the following code:
path = "F:/dir/"
os.system(path[0:2] + " && cd " + path + " && git init")
在我所有的Windows系统,但其中之一运行正常,但在Windows 2003服务器上,它提供了一个目录未找到错误,但如果我从命令提示符下运行相同的命令,它的工作原理。
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.
对不起,如果我的问题以模糊的形式出现,但我完全无法实现。
I'm sorry if my question comes off as vague but I'm totally stumped
推荐答案
os.path 包含许多有用的路径处理功能。可能只是干净地处理路径将解决您的问题。
os.path contains many usefull path manipulation functions. Probably just handling the path cleanly will resolve your problem.
>>> 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
这篇关于在python os.system中通过不同的驱动器字母导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!