Python-> shell:
1. Environment Variables
Copy codeThe 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 ')
Copy codeThe 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
Copy codeThe 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.
Copy codeThe 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
Copy codeThe Code is as follows:
Import OS
Var = '000000'
OS. popen ('wc-C', 'w'). write (var)
Copy codeThe Code is as follows:
Import OS
Var = '000000'
OS. popen ('wc-C', 'w'). write (var)
4. File
Copy codeThe 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 ()
Copy codeThe 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
Copy codeThe Code is as follows:
Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'abc'
Copy codeThe Code is as follows:
Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'abc'
Or
Copy codeThe 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
Copy codeThe 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
Copy codeThe Code is as follows:
Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var
Copy codeThe Code is as follows:
Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var
2.
Copy codeThe Code is as follows:
Import commands
Var = commands. getoutput ('echo abc') # output result
Var = commands. getstatusoutput ('echo abc') # exit status and output result
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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