1

I have a python script that does a zipping of a few files. For zipping the files with a password I use os.system() function and pass my zipping command ie. zip -j -re <file_path> <to_path>. This command asks for a password on the terminal which I have to put manually on the terminal like this.

Enter password: 
Verify password:

I want to hardcode my password and pass it when the terminal asks for a password. I tried to send the passwords using os.system(password) twice thinking that it will take it but it did not work.

cmd = "zip -j -re {} {}".format(DUMPSPATH,TEMPPATH)
passwrd = 'hello123'
os.system(cmd)
os.system(passwrd)
os.system(passwrd)

How do I send arguments using python to fill passwords automatically?

2 Answers 2

1

When you call os.system(), you are sending a command to your shell to run. So calling os.system(passwrd) is sending your password as a standalone command to be run by the shell, which is not what you want here.

The zip program features a -P password argument that allows you to specify the password to encrypt zip files when you initially run the command without having to manually input it for batch jobs. So essentially your code should be changed to something like this:

passwrd = 'hello123'
cmd = "zip -j -re -P {} {} {}".format(passwrd, DUMPSPATH,TEMPPATH)
os.system(cmd)

Another side note, it's recommended to use the subprocess module instead of resorting to os.system, so take a look at subprocess.Popen if you get a chance.

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

Comments

1

It's nice computer_geek has found the correct command line argument (I can't see it listed over here, but it works), but I absolutely HAVE to post my ludicrous solution (for posterity). NOTE - requires xdotool installed on your system.

import subprocess

DUMPSPATH = ""
TEMPPATH = ""
pwd = "lala"

zipper = subprocess.Popen(['zip','-j','-re',DUMPSPATH,TEMPPATH])
subprocess.run(['xdotool','type',pwd])
subprocess.run(['xdotool','key', 'Return'])
subprocess.run(['xdotool','type',pwd])
subprocess.run(['xdotool','key', 'Return'])
zipper.wait()

Comments

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.