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]