#文件操做 open() #open("路徑 + 文件名",」讀寫模式") f=open('filepath','w') #讀寫模式: # r只讀,r+讀寫,w新建(會覆蓋原有文件),a追加,b二進制文件 #經常使用讀寫模式 #如:'rb','wb','r+b'等等 #讀寫模式的類型有: #rU 或 Ua 以讀方式打開, 同時提供通用換行符支持 (PEP 278) #w 以寫方式打開, #a 以追加模式打開 (從 EOF 開始, 必要時建立新文件) #r+ 以讀寫模式打開 #w+ 以讀寫模式打開 #a+ 以讀寫模式打開 #rb 以二進制讀模式打開 #wb 以二進制寫模式打開 #ab 以二進制追加模式打開 #rb+ 以二進制讀寫模式打開 #wb+ 以二進制讀寫模式打開 #ab+ 以二進制讀寫模式打開 #W 若文件存在,首先要清空,而後從新建立文件 #a 把全部的數據追加到文件的尾部,即便seek指在其餘的位置,若是文件不存在,則從新建立 f.read([size]) #size未指定則返回整個文件,若是文件大小>2倍內存則有問題.f.read()讀到文件尾時返回""(空字串) file.readline() #返回一行 file.readline([size]) #返回包含size行的列表,size 未指定則返回所有行 for line in f: print line #經過迭代器訪問 f.write("hello\n") #若是要寫入字符串之外的數據,先將他轉換爲字符串. f.tell() #返回一個整數,表示當前文件指針的位置(就是到文件頭的比特數). f.seek(偏移量,[起始位置]) #用來移動文件指針。偏移量:單位:比特,可正可負 #起始位置:0-文件頭,默認值;1-當前位置;2-文件尾 f.close() #關閉文件 f = open("c:\\temp.txt","r+") #可讀可寫模式 f.write("123") #寫入字符串 f = open("c:\\temp.txt","r") lines = f.readlines() #讀取所有內容 for line in lines print line
練習java
#coding=utf-8
def write_demo ():
f = open("123.dat",'w')
f.write("hello python \n hello java\nhello python \n hello java\nhello python \n hello java\nhello python \n hello java\nhello python \n hello java\nhello python \n hello java\n")
f.close()
def read_demo():
f = open("123.dat",'r')
cont = f.read(11111111)
print(cont)
print("---------")
cont = f.read()
print(cont)
f.close()
#write_demo()
#read_demo()
def readLines_demo():
f = open("123.dat",'r')
cont = f.readlines()
print(type(cont))
print(cont)
#readLines_demo()
def read_bigFile():
f = open("123.dat",'r')
cont = f.read(10)
while len(cont) >0 :
print(cont)
cont = f.read(10)
#read_bigFile()
def read_demo2():
f = open("123.dat",'r+')
#print(f.read())
f.write("aaasdf\n1111111111111111111asdfasdfasdfasdfasdfasdfasdfadsfasdfasfd\nasdfasdfasdfasdfasdfasdf")
print(f.read())
f.close()
#read_demo2()
def copyFile():
f1 = "123.dat"
f2 = "123.dat.bak"
#大文件複製
fs1 = open(f1,'r')
fs2 = open(f2,'w')
cont1 = fs1.readline()
while len(cont1)>0:
#寫入
fs2.write(cont1)
cont1 = fs1.readline()
fs1.close()
fs2.close()
#copyFile()
def read_random():
f=open("123.dat",'r')
str1 = f.read(3)
print("read data is "+str1)
position = f.tell()
print("current position "+str(position))
str1 = f.read(3)
position = f.tell()
print("current position "+str(position))
f.close()
#read_random()
def read_seek():
f=open("123.dat",'a+')
f.seek(5,1)#從當前位置向前偏移5個字節
f.read(3)
print("current position "+str(f.tell()))
f.seek(10,0)#從開始的位置向前都讀取10個字節
print(f.read(10))
print("current position "+str(f.tell()))
f.seek(10,2)#從結束的位置向開始的位置讀取10個字節
print(f.read(10))
print("current position "+str(f.tell()))
f.close()
#read_seek()
import os
def file_rename():
os.rename("123.dat.bak","123.bak")
def file_del():
os.remove("123.dat.bak1")
#file_del()
#copyFile()
#批量重命名文件
def rename_all_file():
files = os.listdir("./")
flag = "_[rename]_"
for f in files:
#print(type(f)) str
pivot = f.find('.')
if pivot < 0:
continue
filename = f[:pivot]
suffix = f[pivot+1:]
if filename.startswith("123") and filename.find(flag) < 0:
os.rename(f,filename+flag+suffix)
#rename_all_file()
def copyFileByBinary(f1,f2):
#大文件複製
fs1 = open(f1,'rb')
fs2 = open(f2,'wb')
cont1 = fs1.read(1024)
while len(cont1)>0:
#寫入
fs2.write(cont1)
cont1 = fs1.read(1024)
fs1.close()
fs2.close()
#
def copy_and_rename():
basedir= "C:/Users/lq/Pictures/tset/"
first_file = "wy0.jpg"
for i in range(1,12):
copyFileByBinary(basedir+first_file,basedir+str(i)+".jpg")
#copy_and_rename()
def file_mkdir():
#os.mkdir("./testdir")
#os.mkdir("./testdir2/inner")
print(os.getcwd())
#file_mkdir()
def join(arr,join_falg):
res = ""
for a in arr:
res += a+join_falg
return res
#遞歸建立文件夾
def file_mkdir2(file):
dirs = file.split("/")
for i in range(1,len(dirs)+1):
d = join(dirs[:i],"/")
if not os.path.exists(d):
os.mkdir(d)
def get_file_parats(file):
path_arr = file.split("/")
last_index = file.rindex("/")
parents_path = file[:last_index]
return parents_path
#刪除全部葉子節點的文件def rm_dirs(path): if os.path.isfile(path): os.remove(path) elif len(os.listdir(path))==0: os.rmdir(path) else: dirs = os.listdir(path) for d in dirs: print(path+"/"+d) if os.path.isfile(path+"/"+d): os.remove(path+"/"+d) elif len(os.listdir(path+"/"+d))==0: os.rmdir(path+"/"+d) else: rm_dirs(path+"/"+d)rm_dirs("./testdir5/inner")