Dhiwakar Ravikumar
Dhiwakar Ravikumar

Reputation: 2207

Escaping Special Characters (*) in Python subprocess.call()

I can't seem to be able to escape these special characters - *. I'm trying to copy some files using a Python script with the following line

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)

But I get the following error at the 2nd line

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

Also I hate passing it using the CSV Style ("arg1","arg2"...). Is there any way of passing it as a raw string?

Upvotes: 2

Views: 2544

Answers (2)

Ivan Klass
Ivan Klass

Reputation: 6627

Better don't do this via subprocess. Believe me it's a bad idea.

For simple filesystem copy operations take a look on python shutil module.

For creating new directories use os.makedirs.

For creating files of certain size see this answers.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

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)

Upvotes: 1

Related Questions