0

I'm currently trying run the shell script by using the os.system method in python.

Python code:

file = open("History.txt","w")
file.write(history)
os.system('./TFPupload.sh')

Shell script code:

#!/bin/sh

HOST="ftp.finalyearproject95.com"
USER='*****'
PASSWD='*****'
FILE='History.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT

echo ">>>uploaded<<<\n"

exit 0

At first, when i tried to run the python code and shell script one by one it works perfectly. However, when i attempt to use python to run shell script, instead of uploading the 'History.txt' file that contains data into the database, the file uploaded is an empty file. When i check using 'nano History.txt', it does contain data, only when it passes the text file to the database will be empty. Why is it?

beginner
  • 1
  • 3

1 Answers1

0

Use With statement to open files whenever possible .

with open("History.txt","w") as file :
  file.write(history)

os.system('./TFPupload.sh')

with statement takes care of closing the fd on its own .

some Ref : What is the python "with" statement designed for?

Community
  • 1
  • 1
Abhimanyu singh
  • 397
  • 3
  • 9