Several Methods for passing python and shell variables to each other

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.