This article mainly introduces how to pass python and shell variables to each other, using methods such as environment variables and pipelines python-> shell:
1. environment variables
The code is as follows:
Import OS
Var = 123 or var = '20140901'
OS. environ ['var'] = str (var) # The key value of environ must be a string.
OS. system ('echo $ var ')
The code is as follows:
Import OS
Var = 123 or var = '20140901'
OS. environ ['var'] = str (var) # The key value of environ must be a string.
OS. system ('echo $ var ')
2. string connection
The code is as follows:
Import OS
Path = '/root/a.txt'
Var = [1]
Var = 'Bash'
OS. system ('echo '+ path) # note that there are spaces after echo
OS. system ('echo '+ str (var [0])
OS. system ('echo '+ var +'/root/c. sh') # note that there are spaces before echo and/root.
The code is as follows:
Import OS
Path = '/root/a.txt'
Var = [1]
Var = 'Bash'
OS. system ('echo '+ path) # note that there are spaces after echo
OS. system ('echo '+ str (var [0])
OS. system ('echo '+ var +'/root/c. sh') # note that there are spaces before echo and/root.
3. MPs queue
The code is as follows:
Import OS
Var = '000000'
OS. popen ('WC-C', 'w'). write (var)
The code is as follows:
Import OS
Var = '000000'
OS. popen ('WC-C', 'w'). write (var)
4. file
The code is as follows:
Output = open ('/tmp/mytxt', 'w ')
Output. write (S) # write string S to the file
Output. writelines (L) # write all the row strings in list L to the file
Output. close ()
The code is as follows:
Output = open ('/tmp/mytxt', 'w ')
Output. write (S) # write string S to the file
Output. writelines (L) # write all the row strings in list L to the file
Output. close ()
5. redirect standard backup output
The code is as follows:
Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'ABC'
The code is as follows:
Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'ABC'
Or
The code is as follows:
Print> open ('/root/a.txt', 'w'), '2017 \ n', 'ABC' # write or generate a file
Print> open ('/root/a.txt', 'A'), '2017 \ n', 'ABC' # append
The code is as follows:
Print> open ('/root/a.txt', 'w'), '2017 \ n', 'ABC' # write or generate a file
Print> open ('/root/a.txt', 'A'), '2017 \ n', 'ABC' # append
Shell-> python:
1. MPs queue
The code is as follows:
Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var
The code is as follows:
Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var
2.
The code is as follows:
Import commands
Var = commands. getoutput ('echo ABC') # output result
Var = commands. getstatusoutput ('echo ABC') # exit status and output result
The code is as follows:
Import commands
Var = commands. getoutput ('echo ABC') # output result
Var = commands. getstatusoutput ('echo ABC') # exit status and output result
3. file
The code is as follows:
Input = open ('/tmp/mytxt', 'r ')
S = input. read () # read the entire file to a string
S = input. readline () # read the next row (the end mark of the row is crossed)
L = input. readlines () # read the entire file to the list of a line string
The code is as follows:
Input = open ('/tmp/mytxt', 'r ')
S = input. read () # read the entire file to a string
S = input. readline () # read the next row (the end mark of the row is crossed)
L = input. readlines () # read the entire file to the list of a line string