Introduction to commonly used files in shell scripts written in Python

Source: Internet
Author: User

Some related regular expressions and some temporary files are often used in shell scripts written in Python.) There are also some related files such as the/etc/passwd file, the following is an introduction to the relevant files we will use in the actual operations of writing shell scripts in gPython.

Sys. argv is a list that stores the command line parameters of the python program. Here, sys. argv [0] is the name of the program.

  • Simple Procedure for generating dll-independent exe using Python
  • Steps required to build a Python Web environment
  • Note the Bug modification and. jam file when getting started with Python
  • Convert a Python script to an executable program in windows
  • Frequently Used functions when writing shell scripts in Python

You can't just say you don't want to practice it. Next we will write a simple script for copying files. My colleague who asked me to write a script two days ago had a directory of tens of thousands of files. He wanted to copy these files to other directories and could not directly copy the directory itself. He tried "cp src/* dest/" and reported a command line too long error. Let me write a script for Python. Start with python:

 
 
  1. import sys,os.path,shutil  
  2. for f in os.listdir(sys.argv[1]):  
  3. shutil.copy(os.path.join(sys.argv[1],f),
    sys.argv[2]) 

Try the post in the linuxapp version-rename all the files in a folder to 10001 ~ 10999. You can write as follows:

 
 
  1. import os.path,sys  
  2. dirname=sys.argv[1]  
  3. i=10001 
  4. for f in os.listdir(dirname):  
  5. src=os.path.join(dirname,f)  
  6. if os.path.isdir(src):  
  7. continue  
  8. os.rename(src,str(i))  
  9. i+=1 

The above is an introduction to the regular expression-related content that is frequently used to compile shell scripts in Python. I hope you will gain some benefits.

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.