0

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

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
cyberoy
  • 363
  • 2
  • 7
  • 18
  • 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? – NightShadeQueen Jul 12 '15 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. – cyberoy Jul 12 '15 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. – NightShadeQueen Jul 12 '15 at 23:03
  • I am trying this because I want to learn it. – cyberoy Jul 12 '15 at 23:16
  • It shows that whenever you run test1.py because of the call to `input("Enter the Zip File:")` test1.py has in it. – martineau Jul 12 '15 at 23:16
  • I know, I am wrong. But, what is the exact way to call it? – cyberoy Jul 12 '15 at 23:20
  • 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. – martineau Jul 12 '15 at 23:22
  • os.system isn't as good as this: http://stackoverflow.com/questions/89228/calling-an-external-command-in-python. But if you are learning, I recommend importing the second file into the first -- it becomes a module, and run it directly – joel goldstick Jul 12 '15 at 23:23
  • possible duplicate of [Run a python script from another python script, passing in args](http://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-args) – wwii Jul 13 '15 at 00:26

1 Answers1

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.

jonnybazookatone
  • 2,188
  • 15
  • 21
  • 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. – NightShadeQueen Jul 13 '15 at 01:22