在Python subprocess.call()中转义特殊字符(*)

时间:2014-11-18 04:20:33

标签: python django

我似乎无法逃脱这些特殊的角色 - *。 我尝试使用带有以下行的Python脚本复制一些文件

subprocess.call(r"mkdir E:\CONTENT\Full1",shell=True)
subprocess.call(r"copy","E:\DATA FOR CONTENT\*.*","E:\CONTENT\Full1",shell=True)
subprocess.call(r"fsutil file createnew ","E:\CONTENT\versionfile.txt","6500000",shell=True)

但是我在第2行遇到以下错误

Traceback (most recent call last):
  File "basic1.py", line 9, in <module>
    subprocess.call(r"copy","E:\DATA FOR CONTENT\*.*","E:\CONTENT\Full1",shell=True)
  File "C:\Python34\lib\subprocess.py", line 537, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python34\lib\subprocess.py", line 767, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

此外,我讨厌使用CSV样式传递它(&#34; arg1&#34;,&#34; arg2&#34; ...)。有没有办法将它作为原始字符串传递?

2 个答案:

答案 0 :(得分:1)

  

args should be a sequence of program arguments or else a single string.

subprocess.call(["copy", r"E:\DATA FOR CONTENT\*.*", r"E:\CONTENT\Full1"], shell=True)

答案 1 :(得分:1)

最好不要通过子进程执行此操作。相信我,这是一个坏主意。

对于简单的文件系统复制操作,请查看python shutil module

要创建新目录,请使用os.makedirs

要创建特定大小的文件,请参阅this answers