目录:
01 | open 函数 | 用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写。 | |
02 | File 对象 | file 对象使用 open 函数来创建。 | |
03 | rename 方法 | 用于命名文件或目录。 | |
04 | remove 方法 | 用于删除指定路径的文件。 | |
05 | mkdir 方法 | 用于以数字权限模式创建目录。 | |
makedirs 方法 | 用于多级创建目录。 | ||
06 | chdir 方法 | 用于改变当前工作目录到指定的路径。 | |
07 | getcwd 方法 | 用于返回当前工作目录。 | |
08 | rmdir 方法 | 用于删除指定路径的目录。 | |
09 | 用于返回指定的文件夹包含的文件或文件夹的名字的列表。 | ||
10 | input 方法 | 接受一个标准输入数据。 | |
11 | raw_input 方法 | 将所有输入作为字符串看待,返回字符串类型。 | |
-------- |
----- |
----------------------------------------------------------------------------------------------------- |
文本文件读写举例:
# 实现功能:从数据中每隔4行选取一个 def a(): fo = open("D:\tmp\a.txt", "r+") fo_w = open("D:\tmp\a1.txt", "w+") lines_new = fo.readlines() i = 0 for l in lines_new: if i%4==0: fo_w.writelines(l[0:-1]) i+=1 fo.close() fo_w.close() # 实现功能:为每一个行文件添加后缀 def c(): fo = open("D:\tmp\a.txt", "r+") fo_w = open("D:\tmp\a2.txt", "w+") lines_new = fo.readlines() for l in lines_new: fo_w.writelines(l+".zip ") fo.close() fo_w.close()
详细说明:
序号 | 类名称 |
功能说明 |
语法 & 举例 | ||
01 | open 函数 |
用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写。 ====<<<< Syntax >>>>==== open (name[, mode[, buffering]]) ====<<<< Parameters >>>>==== ◈ name:一个包含了你要访问的文件名称的字符串值。 ====<<<< mode >>>>==== ◈ r:以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。文件不存在报错。 ====<<<< References >>>>==== 参考:python文件打开方式详解——a、a+、r+、w+区别 |
# 获取栅格数据
arcpy.env.workspace=r"D: 1-Working201720171204-IDL_Average empTSM"
rs = arcpy.ListRasters()
# 遍历栅格数据获取统计信息
# 首先需要建立栅格文件
# 将数据结果保留两位小数
# 输出到txt文档中
fo = open("D:\01-Working\2017\2017_Average\temp\tsm_stats.txt", "w+")
for r in rs:
ro = arcpy.Raster(r)
fo.writelines(ro.name + "
")
fo.writelines("MAX: " + str(round(ro.maximum, 2)) + "
")
fo.writelines("MIN: " + str(round(ro.minimum, 2)) + "
")
fo.writelines("MEAN: " + str(round(ro.mean, 2)) + "
")
fo.close()
读入文件示例: >>> import os >>> fo = open("D:\03-Study\Python\test\test.txt", "r+") >>># 读取全部为一个字符串(read) >>> lines = fo.read() >>> lines 'Line 1: aaaaaa Line 2: bbbbbb Line 3: cccccc Line >>> print lines Line 1: aaaaaa Line 2: bbbbbb Line 3: cccccc >>> fo.tell() 110L >>># 指针返回初始位置 >>> fo.seek(0) >>> fo.tell() 0L >>># 读取数据的一行(readline) >>> lines_new = fo.readline() >>> lines_new 'Line 1: aaaaaa ' >>># 读取剩下的数据为一个列表(readlines) >>> lines_new2 = fo.readlines() >>> lines_new2 ['Line 2: bbbbbb ', 'Line 3: cccccc '] >>> fo.close() 写入文件示例: >>> fo = open("D:\03-Study\Python\test\test.txt", "w+") >>> a = "Alex Lee" >>> b = ["a", "b", "c"] >>> fo.write(a) #写入全部(write) >>> fo.write(" ") #换行符 >>> fo.writelines(b) #写入列表,无换行符(writelines) >>> for i in range(0, len(b)): ... b[i] = b[i] + " " ... >>> print b ['a ', 'b ', 'c '] >>> fo.write(" ") #换行符 >>> fo.writelines(b) #写入列表,有换行符 >>> fo.close() >>> fo = open("D:\03-Study\Python\test\test.txt", "r+") >>> print fo.read() Alex Lee abc a b c |
||
02 | File 对象 |
file 对象使用 open 函数来创建。 ====<<<< Properties >>>>==== ◈ file.name:返回文件的名称。 ====<<<< Methods >>>>==== ◈ file.writelines (sequence):向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。 ====<<<< References >>>>==== |
|||
03 | rename 方法 |
用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError。(相当于移动文件) ====<<<< Syntax >>>>==== os.rename (src, dst) ====<<<< Parameters >>>>==== ◈ src:要修改的目录名。 |
>>> import os >>> os.rename(r"D: est.txt", r"D: est1.txt") >>> os.rename(r"D: est1.txt", r"D: est2.txt") |
||
04 | remove 方法 |
用于删除指定路径的文件。如果指定的路径是一个目录,将抛出OSError。 ====<<<< Syntax >>>>==== os.remove (path) ====<<<< Parameters >>>>====
◈ path:要移除的文件路径。 |
# 实现删除文件夹内的HDF文件 import os rootdir = r"D: mp mp" for file in os.listdir(rootdir): if file.find("hdf") > 0: os.remove(os.path.join(rootdir, file)) |
||
05 | mkdir 方法 |
用于以数字权限模式创建目录。默认的模式为 0777 (八进制)。 ====<<<< Syntax >>>>==== os.mkdir (path[, mode]) ====<<<< Parameters >>>>====
◈ path:要创建的目录。 |
>>> import os >>> os.mkdir(r"D:folder") |
||
makedirs 方法 |
os.makedirs() 方法用于递归创建目录。像 mkdir(), 但创建的所有intermediate-level文件夹需要包含子目录。 ====<<<< Syntax >>>>==== os.makedirs (path[, mode]) ====<<<< Parameters >>>>====
◈ path:要创建的目录。 |
||||
06 | chdir 方法 |
用于改变当前工作目录到指定的路径。(工作空间修改) ====<<<< Syntax >>>>==== os.chdir (path) ====<<<< Parameters >>>>====
◈ path:要切换到的新路径。 |
>>> import os >>> print os.getcwd() D: 1-Working201820180115-浓烟专题图 >>> os.chdir(r"D: 3-StudyPython est") >>> os.rmdir("folder") |
||
07 | getcwd 方法 |
用于返回当前工作目录。(import sys) ====<<<< Syntax >>>>==== os.getcwd () |
|||
08 | rmdir 方法 |
用于删除指定路径的目录。仅当这文件夹是空的才可以, 否则, 抛出OSError。 ====<<<< Syntax >>>>==== os.rmdir (path) ====<<<< Parameters >>>>==== ◈ path:要删除的目录路径。 |
|||
09 | listdir 方法 |
用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 它不包括 '.' 和'..' 即使它在文件夹中。 ====<<<< Syntax >>>>==== os.listdir (path) ====<<<< Parameters >>>>==== ◈ path:需要列出的目录路径。 |
>>> import os >>> print os.listdir(r"D: est") ['11.txt', 'cnblog1.txt', 'frame.ui'] |
||
10 | input 方法 |
Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型。 注意:input() 和 raw_input() 这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。 ====<<<< Syntax >>>>=== input ([prompt]) ====<<<< Parameters >>>>==== ◈ prompt:可选,字符串,可作为一个提示语。 |
>>> a = input("input:") input:123 >>> type(a) <type 'int'> >>> a 123 >>> a = input("input:") input:"alex" >>> a 'alex' >>> a = input("input:") input:alex Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> a = input("input:") File "<string>", line 1, in <module> NameError: name 'alex' is not defined |
||
11 | raw_input 方法 |
用来获取控制台的输入。 ====<<<< Syntax >>>>==== raw_input ([prompt]) ====<<<< Parameters >>>>==== ◈ prompt:可选,字符串,可作为一个提示语。 |
>>> a = raw_input("input:") input:123 >>> type(a) <type 'str'> >>> a '123' >>> a = raw_input("input:") input:alex >>> a 'alex' |
||
-- |
----- |
-------------------------------------------------- |