Io鈥斺�旀枃浠剁殑鎿嶄綔

涓�.甯歌鐨勬ā寮�


Io鈥斺�旀枃浠剁殑鎿嶄綔_第1张图片

浜�.渚嬪瓙濡備笅

# r妯″紡

file=open('haha.html','r',encoding='gbk')

f=file.read()

print(f)

file.close()

#聽w妯″紡

file=open('wenjian.txt','w')

file.write('绐楀墠鏄庢湀鍏�')

file.close()

#聽a妯″紡

file=open('wenjian.txt','a')

file.write('绐楀墠鏄庢湀鍏�,鐤戞槸鍦颁笂闇溿��')

file.close()

#聽w+妯″紡

file=open('wenjian.txt','w+')

file.write('涓惧ご鏈涙槑鏈堬紝')

file.seek(0)

a=file.read()

print(a)

file.close()

#聽r+妯″紡

file=open('wenjian.txt','r+')

file.write('浣庡ご鎬濇晠涔�')

file.seek(0)

a=file.read()

print(a)

file.close()

#聽a+妯″紡

file=open('haha.html','a+')

file.write('闀块楣跨殑鑴栧瓙闀�')

file.seek(0)

a=file.read()

print(a)

file.close()

#聽rb妯″紡

file=open('haha.html','rb')

a=file.read()

print(a.decode('gbk'))

file.close()

#聽wb妯″紡

file=open('haha.html','wb')

a='澶圭潃鐏溅涓婇潰鍖�'

a=a.encode('gbk')

file.write(a)

file.close()

#聽ab妯″紡

file=open('haha.html','ab')

a='澶圭潃鐏溅涓婇潰鍖�'

a=a.encode('gbk')

file.write(a)

file.close()

#聽rb+妯″紡

file=open('haha.html','rb+')

a='涓変汉琛屽繀鏈夋垜甯�'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

#聽wb+妯″紡

file=open('haha.html','wb+')

a='涓変汉琛屽繀鏈夋垜甯�11'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

'''

#聽ab+妯″紡

file=open('haha.html','wb+')

a='涓変汉琛屽繀鏈夋垜甯�111'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()


娉ㄦ剰锛�

.write()妯″紡锛屄峰鏋滄枃浠朵笉瀛樺湪閭d箞鍒涘缓锛屽鏋滃瓨鍦ㄩ偅涔堝氨鍏堟竻绌猴紝鐒跺悗鍐欏叆鏁版嵁銆�

涓�.璺緞銆俛=f.write('hello world, i am here!')聽 print(a) 杩欐牱鍐欒繑鍥炲�兼槸闀垮害銆�


涓�.璺緞闂

鍦╳indows涓嬶細

Io鈥斺�旀枃浠剁殑鎿嶄綔_第2张图片

鍥�.璇绘暟鎹�

锛堜竴锛� readline锛�

file=open('wenjian.txt','r+')

a=file.write('绐楀墠鏄庢湀鍏�,\n鐤戞槸鍦颁笂闇�,\n')

print(file.readline())

print(file.readline())

file.close()


鎴栬�咃細

file=open('wenjian.txt','r+')

a=file.write('绐楀墠鏄庢湀鍏�,\n鐤戞槸鍦颁笂闇�,\n')

n=1

for i in a :

print(%s:%s)%(n,i)

n+=1

file.close()

锛堜簩锛� readlines锛�

f = open('test.txt','r')

content聽=聽f.readlines()

print(type(content))

i=1

fortempincontent:

print("%d:%s"%(i,聽temp))

i+=1

f.close()

浜�.鑾峰彇鏂囦欢浣嶇疆锛坱ell()锛�

f = open("test.txt","r")

str聽=聽f.read(3)

position = f.tell()

print("褰撳墠鏂囦欢浣嶇疆:聽",聽position)

鍏�.瀹氫綅鏂囦欢浣嶇疆锛坰eek()锛�

1.from:鏂瑰悜

a)0:琛ㄧず鏂囦欢寮�澶达紙python3锛�

b)1:琛ㄧず褰撳墠浣嶇疆锛坧ython2锛�

c)2:琛ㄧず鏂囦欢鏈熬锛坧ython2锛�

f = open("test.txt","r")

str聽=聽f.read(30)

print("璇诲彇鐨勬暟鎹槸:聽",聽str)

#鏌ユ壘褰撳墠浣嶇疆

position聽=聽f.tell()

print("褰撳墠鏂囦欢浣嶇疆:聽",聽position)

#閲嶆柊璁剧疆浣嶇疆

f.seek(5,0)

#鏌ユ壘褰撳墠浣嶇疆

position聽=聽f.tell()

print("褰撳墠鏂囦欢浣嶇疆:聽",聽position)

f.close()

鍥�.os妯″潡

锛堜竴锛�:鏂囦欢鐨勯噸鍛藉悕銆佸垹闄�


锛堜簩锛�:鍒涘缓鏂囦欢澶�


锛堜笁锛夎幏鍙栧綋鍓嶇洰褰�

importos

os.getcwd()

锛堝洓锛夋敼鍙橀粯璁ょ洰褰�

importos

os.chdir("../")

锛堜簲锛夋敼鍙橀粯璁ょ洰褰�

importos

os.listdir("./")

锛堝叚锛夊垹闄ゆ枃浠跺す

importos

os.rmdir("寮犱笁")

import聽shutil

os.rmtree(鈥榤鈥�)

浜�.os.path妯″潡

os.path.abspath(path) #杩斿洖缁濆璺緞


os.path.split(path)

灏唒ath鍒嗗壊鎴愮洰褰曞拰鏂囦欢鍚嶄簩鍏冪粍杩斿洖銆�

>>> os.path.split('c:\\csv\\test.csv')

('c:\\csv', 'test.csv')

>>> os.path.split('c:\\csv\\')

('c:\\csv', '')

os.path.dirname(path)

杩斿洖path鐨勭洰褰曘�傚叾瀹炲氨鏄痮s.path.split(path)鐨勭涓�涓厓绱犮��

>>> os.path.dirname('c:\\csv\test.csv')

'c:\\'

>>> os.path.dirname('c:\\csv')

'c:\\'

os.path.exists(path)

濡傛灉path瀛樺湪锛岃繑鍥濼rue锛涘鏋減ath涓嶅瓨鍦紝杩斿洖False銆�

>>> os.path.exists('c:\\')

True

>>> os.path.exists('c:\\csv\\test.csv')

False

os.path.isabs(path)

濡傛灉path鏄粷瀵硅矾寰勶紝杩斿洖True銆�

os.path.isfile(path)

濡傛灉path鏄竴涓瓨鍦ㄧ殑鏂囦欢锛岃繑鍥濼rue銆傚惁鍒欒繑鍥濬alse銆�

>>> os.path.isfile('c:\\boot.ini')

True

>>> os.path.isfile('c:\\csv\\test.csv')

False

>>> os.path.isfile('c:\\csv\\')

False

os.path.isdir(path)

濡傛灉path鏄竴涓瓨鍦ㄧ殑鐩綍锛屽垯杩斿洖True銆傚惁鍒欒繑鍥濬alse銆�

>>> os.path.isdir('c:\\')

True

>>> os.path.isdir('c:\\csv\\')

False

>>> os.path.isdir('c:\\windows\\test.csv')

False

你可能感兴趣的:(Io鈥斺�旀枃浠剁殑鎿嶄綔)