One . Python Execute system commands

Python have access to system and popen To execute system commands , You need to import os, The parameters passed in are all one character , But there are some differences between the two .

os.system(cmd): Return is a status code , When called, it will automatically print out the output during execution

os.popen(cmd): What is returned is a readable object , Calling the read Method can print out the output information during execution

os.system("dir")

os.popen("dir"), Need to call read Method to print out

  Two . Python readlines

I used the input method read(), readlines() When reading lines from a file ,python Line terminators are not automatically removed , It needs to be removed manually by the programmer himself .

with open('myfile', 'r') as f:
lines = [line.strip() for line in f.readlines()]

call str.strip() You can remove the beginning and end of a string whitespace.

strip([chars]): Remove some of the first and last characters , And back to . If chars by None, What is removed is whitespace(\n, \r, \t etc. )

>>> ' spacious '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

lstrip(): Remove some of the first characters

rstrip(): Remove some of the characters at the end

splitlines([keepends]): according to line breaks Cut string , Return a list . If keepends by True, Will keep line breaks.

Python More miscellaneous related articles

  1. python miscellaneous

        Python/JS/    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  2. Four 、python miscellaneous

    One .pycharm Single line and multi line comment shortcuts                         Multiple lines of comments are just one key combination : Choose +Ctrl+/

  3. Miscellaneous python Descriptor protocol

    Miscellaneous python Descriptor protocol Content of this section origin Descriptor protocol concept Class static method and class method implementation principle Class is used as a decorator 1. origin I have nothing to do with it django The built-in paging method in , It is found that classes are used as decorators ...

  4. Miscellaneous python utilize pycrypto Realization RSA

    Miscellaneous python utilize pycrypto Realization RSA Content of this section pycrypto Module introduction RSA Public private key generation of RSA Encrypt data using public key RSA Use the private key to decrypt ciphertext Break blog Park landing pycrypto Module introduction py ...

  5. 《python Programming core 》 note —— miscellaneous

    python Statement adds a newline character to each line by default , Just adding a comma at the end can change this behavior If there's no return It will automatically return to None object PEP(python The enhancement proposal is abbreviated as )http://python.org ...

  6. Python Learning notes ( 5、 ... and ) miscellaneous

    1. Assert assert len(unique_characters) <= 10, 'Too many letters' #… Equivalent to : if len(unique_characters ...

  7. python Basics -- miscellaneous

    String formatting : ython There are two ways to format a string of :  Percentage method .format The way The percentage sign is relatively old , and format The way is more advanced , Trying to replace the old way , At present, the two coexist .[PEP-310 ...

  8. Python Learn miscellaneous

    #print("Hello World!") #name = "nihfjkds" age = 454 num1 = 1 num2 = 2 #print(nam ...

  9. Experiment - miscellaneous -WTF?(python 01 Code to picture )

    It's a new type . code by base64 code , The transcoding is 01 Code , Direct blinding , Check out the relevant wp I know it's the picture . Copy it to the editor and you can see a total of 65536 A digital , The prescription is 256, So this is a square picture –> ...

Random recommendation

  1. java Background check format

    package com.hengxin.qianee.utils; import java.net.InetAddress; public class RegexUtils { /** * Whether the user name is ...

  2. PCL still SAP?

    When you first created the Hello solution in Visual Studio, you had a choice of two application temp ...

  3. use MapX And C# Developing geographic information systems

    turn :http://www.cnblogs.com/dachie/archive/2010/08/17/1801598.html Chapter four MapX And C# example ... 5 4.1 MapX Layer building ... 5 ...

  4. HTML+CSS+JS Learning summary

    HTML: What is? HTML? HTML It's a language used to describe Web pages . HTML Hypertext markup language (Hyper Text Markup Language) HTML Not a programming language , It's a marker ...

  5. [ turn ] Chapter one Windows Shell What is it? 【 source :http://blog.csdn.net/wangqiulin123456/article/details/7987862】

    A good definition of an operating system shell is that it is a user interface provided by the system , It allows users to perform common tasks , Such as accessing the file system , Export executor , Change system settings, etc .MS-DOS There is one Command.COM Playing the role . However Windo ...

  6. SqlDataReader Get the return value of the stored procedure

    Writing stored procedures , Cannot get return value Attach code : SqlDataReader reader = null;// totalRecords = ; try { SqlConnectionHolder conne ...

  7. MySQL Quick start Basic skills

    Before that : I've been using MSSERVER, Just used MySQL There are a lot of maladjustments . Here is a summary of the problems and scenarios encountered in the work , The limitations of this paper are welcome to point out MySQL There's client-side (SQLyog), Pull and write code : perhaps ...

  8. Based on Alibaba cloud JavaEE Introduction to the system framework

    Outlook of system framework based on alicloud 1) CDN For caching static files and so on . Seven cattle and Ali's are OK . Seven cows need to do it a little longer , The interface of various image processing should be improved Ali's CDN A little better , But there is no insecure access , There's not a little bit of ...

  9. OPSF - 2, State machine

    1, Message update address       Point to point : All messages sent 224.0.0.5      Virtual link : Unicast address       On the broadcast network :DR OTHER to DR/BDR 224.0.0.6,DR/BDR to DR OTEHER 2 ...

  10. Curator And Recipes The lock of

    Reprinted from :https://blog.csdn.net/kiss_the_sun/article/details/50221463 Reference documents : http://ifeve.com/java_lock_see ...