Python file replace [go]

Source: Internet
Author: User

File Replace

Replacestrbatch

Role:

Bulk replaces $hadoop_home$ in all files in the specified directory with/home/ocetl/app/hadoop

Knowledge Points:

1. Def listfiles (Dirpath):

Defining functions

2. filelist=[]

Declaring an array

3. For root,dirs,files in Os.walk (Dirpath):

Os.walk ("")

Returns a ternary group, the traversed path, the directory under the current traversal path, the file name under the current traversal directory

4. Os. Path.join (Root,fileobj)

directory = ["C", "pic", "18x.jpg"]
Os.path.join (*directory) #相当于os. Path.join ("C", "pic", "18x.jpg")

5. Regex = ur ' func_sys_add_accdetail '

Regex=ur "

"#正则表达式

This example does not add this regular, does not affect the running result

6. F = open (Fileobj, ' r+ ')

r+: Readable and writable, if the file does not exist, error
w+: Readable and writable, if the file does not exist, create

7. All_the_lines=f.readlines ()

. ReadLines () reads the entire contents of the file per row, puts the contents of the read into a list, and returns the list type

7. F.seek (0)

The Seek () function goes back to the file header because the iterator has accessed all the rows of the file

8. F.truncate ()

Empty file contents

This function can only be performed with files opened in writable mode such as "r+" "rb+" "W" "WB" "wb+"

9. If __name__== ' __main__ ':

In the If __name__ = = "__main__": After the statement as the module is called, the code after the statement is not executed;

When used directly, the code after the statement executes. Typically, this statement is used in module tests

Python file replace [go]

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.