python file replace [转]

file replace

ReplaceStrBatch

作用:

批量将指定目录下的所有文件中的$HADOOP_HOME$替换成/home/ocetl/app/hadoop

知识点:

1. def listFiles(dirPath):

定义函数

2. fileList=[]

声明数组

3. for root,dirs,files in os.walk(dirPath):

os.walk("")

返回一个三元组,遍历的路径、当前遍历路径下的目录、当前遍历目录下的文件名

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"

" #正则表达式

本例中不加这个正则,不影响运行结果

6. f = open(fileObj,‘r+‘)

r+:  可读可写,若文件不存在,报错

w+: 可读可写,若文件不存在,创建

7. all_the_lines=f.readlines()

.readlines()每次按行读取整个文件内容,将读取到的内容放到一个列表中,返回list类型

7. f.seek(0)

seek()函数回到文件头部,因为迭代器已经访问完了文件的所有行

8. f.truncate()

清空文件内容

仅当以 "r+"   "rb+"    "w"   "wb" "wb+"等以可写模式打开的文件才可以执行该功能

9.  if __name__==‘__main__‘:

在if __name__ == "__main__":之后的语句作为模块被调用的时候,语句之后的代码不执行;

直接使用的时候,语句之后的代码执行。通常,此语句用于模块测试中使用

时间: 2024-08-17 06:15:55

python file replace [转]的相关文章

[改]在windows右键菜单中加入“新建Python File文件”并创建模板

1.首先写好模板文件,随便保存在一个地方,比如我是"D:\Python27\foo.py"; 2.打开注册表(regedit),找到 [HKEY_CLASSES_ROOT] -> [.py] (没有的话,自己新建项.py); 3.在 [.py] 下新建项 [ShellNew] (已经有的话就删掉重建); 4.在 [ShellNew] 下新建 字符串值 ,名称为 FileName ,键值为模板文件的绝对路径,比如我的是 D:\Python27\foo.py ; 在右键新建菜单中就会

Python File I/O

File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk). Since, random access memory (RAM) is volatile which loses its data when computer is turned off, we use file

File.Delete(), File.Copy(), File.Replace()

<1> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //-----------------------------------WriteAllText()--------------------------

Python - File - 第十八天

Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法. open() 函数常用形式是接收两个参数:文件名(file)和模式(mode). open(file, mode='r') 完整的语法格式为: open(file, mode='r', bufferi

python file模块 替换输入内容脚本

1 [email protected]:/home/liujianzuo/python/test# ls 2 passwd rc.local test1 3 [email protected]:/home/liujianzuo/python/test# py test1 -r EXIT exit /home/liujianzuo/python/test/rc.local 4 共修改了0行. 5 [email protected]:/home/liujianzuo/python/test# py

python file operations

python_files_operations files, file objects open(name [, mode [, bufsize]]) eg: file = "data.txt" f = open(file, 'r') f = open(file, 'w') 'r':read 'w':write 'a':append 'rb':write binary,在unix中文件都可以当作二进制,所以都用'rb' 'U' or 'rU': 提供通用支持给换行符,可以写跨平台执行的

Python:file (read,readline,readline )使用方法

Python读取文件时,在使用readlin.readlines时会有疑惑,下面给大家详解:一.例:a.txt的内容为    aaa 123    bbb 456二.首先我先设置个变量:    a="a.txt"    c=file(a)三.此时我们分别看下使用read.readline.readlines 的读取结果:  (1).read:        IN: c.read()        OUT: ''      SO: read每次读取文件时,通常将读取到底文件内容放到一个字

Python file 方法

#!/usr/bin/env python # *_* coding=utf-8 *_* """ desc: 文件方法 ############################# file.read()         #read([size]) -> read at most size bytes, returned as a string. file.readline()     readline([size]) -> next line from the f

python file

1 >>> help(open) 2 Help on built-in function open in module __builtin__: 3 4 open(...) 5 open(name[, mode[, buffering]]) -> file object 6 7 Open a file using the file() type, returns a file object. This is the 8 preferred way to open a file. S