1

Suppose I have a Zip file and test1.py is taking the input of the Zip file from the user.

Now, test2.py prints the contents of a Zip file.

I want to take the input Zip file from test1.py for the operation of test2.py.

So whenever I run the test1.py, it should ask for the name of Zip file and forward it to the test2.py and show the output of test2.py.

I am trying to do it like this :

test1.py:

import zipfile
import os

inputzip = input("Enter the Zip File:")
file = zipfile.ZipFile(inputzip, "r")
os.system("py test2.py")

test2.py:

import zipfile
import os

print ("The list of files in the zip : ")

for name in file.namelist():
   print (name)

But whenever I am going to run test1.py, it is always showing,

Enter the zip file:

test2.py is not executing. I am using python 3.x

9
  • Can I ask why you're doing this in this way? Instead of, say, importing one file from another, or having these two be one file? Commented Jul 12, 2015 at 22:56
  • I want to run a single file. It should just take the input from user. Then it will forward it to second file for further processing (it will call the second file) and second file's output will be shown. Commented Jul 12, 2015 at 23:00
  • Yes. I got that part. What I don't get is exactly why you're doing this, unless this is an exercise in passing things from one program to another, because this seems just about the wrong way to go about things. Commented Jul 12, 2015 at 23:03
  • I am trying this because I want to learn it. Commented Jul 12, 2015 at 23:16
  • 1
    You can't pass a ZipFile from one script to another. You could pass the name of the file by using os.system("py test2.py " + inputzip) and then modifying test2.py to expect a command line argument (in sys.argv[1]) that contains the file name. Commented Jul 12, 2015 at 23:22

1 Answer 1

2

The commenters are right, but it sounds like you want to know anyway. The problem was already highlighted by @martineau. What is happening is the following:

  1. test1.py executes in its own process and opens the zip file.
  2. test1.py asks the os to start test2.py
  3. test2.py executes in its own process, and has no concept of the zip file that exists in the child process of test1.py

Because they do not know about each other, the easiest option you have in the manner you want to do it (i.e., using os.system()), is to pass information from test1.py to test2.py in the form of a string. One way would be the following:

test1.py:

import zipfile
import os

inputzip = input("Enter the Zip File:")
files = zipfile.ZipFile(inputzip, "r")
name_list = (" ".join([i for i in files.namelist()]))
os.system("python3 test2.py {0}".format(name_list))

and

test2.py:

import zipfile
import sys

print ("The list of files in the zip : ")

for name in sys.argv[1:]:
   print (name)

output:

Enter the Zip File:test.zip
The list of files in the zip : 
test
txt1
txt2

In the end this is pretty messy and as already pointed out by @joelgoldstick, you should just import the method from test2.py into test1.py. For example:

test2.py:

def test2_method(zipfile):
  print ("The list of files in the zip : ")

  for name in zipfile.namelist():
     print (name)

test1.py:

import zipfile

from test2 import test2_method

inputzip = input("Enter the Zip File:")
files = zipfile.ZipFile(inputzip, "r")

test2_method(files)

output:

Enter the Zip File:test.zip
The list of files in the zip : 
test
txt1
txt2

I am currently wondering how easy it would be to have both processes share memory and one to be able to pass the memory reference of the opened zip file object to the other. Don't know if this is even possible >_<. Anyone who could enlighten would be cool.

Sign up to request clarification or add additional context in comments.

1 Comment

Another thing you can do is have the first script write to the stdin of the second script. Or you can pass around pickles. Or json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.