简体   繁体   English

来自外部驱动器的Subprocess.call()

[英]Subprocess.call() from external drive

I'm trying to run a Python script off of an external drive. 我正在尝试从外部驱动器运行Python脚本。 The script runs fine, but when it reaches the line subprocess.call(callThis, shell=True) , I get the error (from command prompt) the filename, directory name, or volume label syntax is incorrect . 该脚本运行正常,但当它到达subprocess.call(callThis, shell=True)subprocess.call(callThis, shell=True) ,我得到错误(从命令提示符) the filename, directory name, or volume label syntax is incorrect When I run the same script off of the C drive, it works fine and copies the files. 当我从C驱动器运行相同的脚本时,它工作正常并复制文件。 Python is installed on the C drive, and the external drive is a microSD card if it matters. Python安装在C盘上,外部驱动器是microSD卡,如果重要的话。 Python version is 3.4, but it needs to work on Python 2.7 as well. Python版本是3.4,但它也需要在Python 2.7上运行。

Relevent code: 相关代码:

paths = [os.path.join(dirpath, fname) for dirpath, __, fnames in os.walk('.\\MUSIC') for fname in fnames]
for path in paths:
    pathcomp = path.replace('.\\MUSIC', 'C:\\redacted\\Music\\MUSIC')
    if not os.path.isfile(pathcomp):
        abspath = os.path.abspath(path)
        callThis = 'copy "'+abspath+'" "'+os.path.dirname(pathcomp)+'"'
        print(callThis)
        subprocess.call([callThis], shell=True)

printing callThis outputs something similar to 打印callThis输出类似的东西

copy "G:\MUSIC\dir1\dir2\fname.mp3" "C:\redacted\Music\MUSIC\dir1\dir2\fname.mp3"

Typing that exact line into command prompt works fine and copies the file. 在命令提示符中键入该确切的行正常工作并复制该文件。

I think the problem has something to do with the command prompt running out of the C drive instead of the external drive. 我认为这个问题与命令提示符运行C驱动器而不是外部驱动器有关。 How can I make it run out of the external drive (show G:/> instead of C:\\ in the python script? I tried `subprocess.call('G:', shell=True) but have the same problem. 如何让它用完外部驱动器(在python脚本中显示G:/>而不是C:\\ ?我试过`subprocess.call('G:',shell = True)但是有同样的问题。

Passing a list to subprocess.call overrides the shell tokenization rules. 将列表传递给subprocess.call会覆盖shell标记化规则。 You should pass a string to the call function instead. 您应该将字符串传递给call函数。

subprocess.call(callThis,shell=True)

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

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