Python executes the shell-subprocess ModulePython executes shell-subprocess module call
Retcode = subprocess. call (["ls", "-l"])Call I feel like OS. popen. I can only execute the command to check the returned value. Command output is not obtained. So we still need to use Popen. Note that P is capitalized.Popen basic usage:
Child1 = subprocess. Popen (["cat", "
handle non-file-name strings.#example.py##Example of using Shell-wildcard style matching in list comprehensions fromFnmatchImportFnmatchcase as Matchaddresses= [ '5412 N CLARK ST', '1060 W ADDISON ST', '1039 W GRANVILLE AVE', '2122 N CLARK ST', '4802 N BROADWAY',]a= [addr forAddrinchAddressesifMatch (addr,'* ST')]Print(a) b= [addr forAddrinchAddressesifMatch (addr,'54[0-9][0-9] *clark*')]Print(b)>>> ================================ RES
Several Methods for python to execute shell commands
1. OS. system ()
Advantages: it is simple and can be used on linux and widnows platforms. You only need to determine whether the returned result is 0 or 1 to determine whether the execution is successful.
Disadvantage: The returned output cannot be obtained.
Example:
os.system('ls')
2. OS. popen ()
Advantage: output results can be obtained.
Disadvanta
Sys.path.append (' E:/projects/djangoprojects/myfirstsite ') Os.environ.setdefault (' Django_settings_module ', ' myfirstsite.settings ') From books.models Import * Print (Author.objects.all ()) as follows (Http://stackoverflow.com/questions/8047204/django-script-to-access-model-objects-without-using-manage-py-shell): Since Django 1.4 You should avoid using setup_environ(settings) (post by Melug) because it is deprecated. Use the following instead
The existing plain text titled Test.txt, which counts the number of occurrences of the word.Content of Test.txt:I have the application Someday Oneday day DemoI have some one coma ideal naive ICode implemented in Python:Import reCount = {}f = open (' Test ', ' R ')b = F.read ()#print bcd = Re.split (' [\\n]+ ', b) #注意split的用法Print CDFor I in CD:Count[i] = Count.get (i,0) + # Note the use of the Get () methodPrint CountResults obtained after executing the code:[' I ', ' have ', ' has ', ' applicat
It is not so willful to find the monitor to refresh files in real time during the work process.So a python script similar to tail is made with tail in the shell.Details are as follows:1 #!/usr/bin/env python2 #Coding=utf-83 Importos,sys,time,getopt4 5Lastline="'6linelist=[]7 defgetnewline (filename, count):8 Globallastline9 GloballinelistTenCmd='Tail-'+count+' '+filename One while(1): A #line5=os.popen (' tail-5 speedy_result.txt
An idea:
1 python2.7
2 Python web framework Bottle
3 Shell Scripting Management KVM
Together can be a ~
This time I will use bottle simple implementation of management KVM host, open source out of the ~ performance rotten words, don't blame me ~
Management KVM host can be completed on the page, a foreign person with Django wrote this set of procedures, feeling good, just a little more complex, a bit m
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundleTouch ~/.VIMRCThen paste the attached file into the ~/.VIMRC.Vim has just entered the error, we can press ENTER.Then enter: Plugininstall can be installed, installation process such as:650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/91/wKioL1g0P5uAjV-XAAA6gPTq8vI501.png "title=" Dsfsdfdfs.png "alt=" Wkiol1g0p5uajv-xaaa6gptq8vi501.png "/>Download time is a bit long, depending on the speed of the specific. After the a
file1PHP2 $filename= "Input_file_name";3 4 $fp=fopen($filename, "R");5 if(!$fp){6 EchoThe Open File$filenamefailed\n ";7 Exit(1);8 }9 Else{Ten while(!feof($fp)){ One //fgets (file,length) does not specify a length of 1024 bytes by default A $buf=fgets($fp); - - $buf=Trim($buf); the if(Empty($buf)){ - Continue; - } - Else{ + Echo $buf." \ n "; - } + } A fclose($fp); at } -?>View CodeReading stan
Shell:Log_date= ' date-d "1 day Ago" +%y-%m-%d ' #以指定格式设置一天前的年份月份日期aws S3 ls $LAST 5_base_path > ${tmp_file} #将s3这个位置的文件清单输出到tmp . txt files if grep-q $LOG _date $TMP _file; Then #查找是否存在spark-submit--master "local[*]"/root/downloads/packagestat/selecttopnapppackage.py $TEST _1 $TEST _2 $ Android_output_base_path $IOS _output_base_path #提交py文件加输入参数Python:Eval function: Convert the string "{Key:value}" to a dictionaryDatetime.strptime (date_string, format): Converts a format string to a DateTime o
Python multi-thread and queue management shell program, pythonshell
First, describe the environment. There are many JAVA programs on the machine. We have configured a startup | stop | restart script in each JAVA program.
For example:
Now we need to run these scripts at the same time to quickly start all JAVA programs. If we only use multithreading, the thread will not return messages to the parent process,
Code1f = open ('Yesterday','R', encoding='Utf-8')2F2 = open ('Yesterday.bak','W', encoding='Utf-8')3OLD_STR = input ('Please enter the characters you want to modify:')4REPLACE_STR = input ('Please enter a replacement character:')5 forLineinchf.readlines ():6line =line.replace (OLD_STR,REPLACE_STR)7 Print(line)8 F2.write (line)9 f.close ()TenF2.close ()File contents: (Yesterday)Somehow, it seems the love I knew is always the most destructive kind I don't know why, I went through the most dev
It is not intended to look at this problem from the perspective of the language's completeness, but only in the sense of the user:
The "base function point" of shell script (which refers to the script support provided by SH, csh, bash, zsh, etc.) is the use of external commands. In its internal data processing, emphasis is placed on text (or string, or character stream) processing, and weak on operations and other basic data structures (such as comm
A recent requirement is to execute the shell command on the page, the first thought is Os.system,
The code is as follows
Copy Code
Os.system (' Cat/proc/cpuinfo ')
But the results of a printed command on the page 0 or 1, of course, do not meet the requirements.
Try the second scenario Os.popen ()
The code is as follows
Copy Code
Output = Os.popen (' Cat/proc/cpuinfo ')Print Output.re
terminalYou can use shell to start the Scrapy terminal:Is the address of the webpage you want to crawl.Scrapy Shell Print log:Scrapy Shell ' http://scrapy.org 'Do not print logs:Scrapy Shell ' http://scrapy.org '--nolog Using the terminalD:\ Project \ Small Project \scrapy_day6_httpbin\httpbin>scrapy
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.