user1245262
user1245262

Reputation: 7505

Am I using python's subprocess.call correctly?

I'm trying to create a series of tar files from within Python code. As a practice I have a subdirectory - 'O', which contains the files I want to tar. When I try typing

tar -cf O.tar ./O

from the command line, things work. But, when I enter the Python interpreter and enter

import subprocess
subprocess.call(["tar","-cf O.tar ./O"])

I get the following error:

tar: Cowardly refusing to create an empty archive

Does this make sense? I tried

import os
os.listdir(".")

To make sure I could still see my "O" subdirectory from within the Python shell, and I can.

What am I missing here?

Upvotes: 0

Views: 776

Answers (2)

user5397243
user5397243

Reputation:

it will work run:

import subprocess
import sys
proc = subprocess.Popen(["tar","-cf O.tar ./O"],stdin=subprocess.PIPE)
proc.communicate()

if you can execute some other command or program use sys.executeable()

Upvotes: -1

yutaka2487
yutaka2487

Reputation: 1946

maybe.

import subprocess
subprocess.call(["tar","-cf", "O.tar", "./O"])

Upvotes: 3

Related Questions