如题所述
1 æ件读åå
¨ææ¬æä½
å¨ä¸å®åºæ¯ä¸æ们éè¦æææ¬å ¨é¨å 容读ååºæ¥ï¼è¿è¡å¤çãpythonæä¾ä¸ç§å½æ°è¯»åæ件ï¼åå«æ¯read readline readlinesï¼
read()ï¼è¯»åæ件çå ¨é¨å 容ï¼å ä¸åæ°å¯ä»¥æå®è¯»åçå符ã
readline()ï¼è¯»åæ件çä¸è¡ã
readlines()ï¼è¯»åæ件çææè¡å°å åä¸ã
ä¸ååºæ¯ä¸æ们å¯ä»¥éæ©ä¸åå½æ°å¯¹æ件è¿è¡è¯»åã
1.1 æ¹æ³ä¸
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")
txt=file.read()
# å ¨ææ¬çå¤ç
file.close()
使ç¨readå½æ°å°æ件ä¸çå å®¹å ¨é¨è¯»åï¼æ¾å¨å符串åétxtä¸ãè¿æ ·æä½éåäºææ¬è¾å°ï¼å¤çç®åçæ åµï¼å½æ件è¾å¤§æ¶ï¼è¿ç§æ¹å¼å¤çæ¶ä¸åéçãä¸æ¬¡æ§è¯»åè¾å¤§çæ件å°å åä¸ï¼ä¼èè´¹è¾å¤çæ¶é´åèµæºãè¿æ¶ååæ¹å¤çæææ´å¥½ã
1.2 æ¹æ³äº
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")
txt= file.read(4)
# ææ¬çå¤çwhile txt != ""txt= file.read(4)
# æ¹éææ¬å¤ç
file.close()
è¿ç§æ¹æ³éåäºåæ¹å¤çææ¬ä¿¡æ¯ï¼æ¯æ¬¡æ¹éè¯»å ¥ï¼æ¹éå¤çï¼ä¸ä¼å¯¹å åé æè¾å¤§çååã
1.3 æ¹æ³ä¸
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")for line infile.readlines():
# å¤çæ¯ä¸è¡æ°æ®
file.close()
è¿ç§å¤çæ¹å¼éåå¤ç以è¡ä¸ºåå²ç¹ç¹çææ¬ï¼å¹¶ä¸ææ¬è¾å°ï¼å 为è¿ç§å¤çæ¹å¼éè¦ä¸æ¬¡æ§ææ件ææå 容读åå°å åä¸ã
1.4 æ¹æ³å
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r") # è¿éçfileæ¶æ件å¥æfor line infile:
# å¤çæ¯ä¸è¡æ°æ®
file.close()
è¿ç§æ¹å¼åæ¹æ³ä¸ä¸çåºå«æ¯åè¡è¯»å ¥ï¼éè¡å¤çï¼ä¸ä¼ä¸æ¬¡æ§ææ件ææå 容é½è¯»å ¥å°å åä¸ï¼å¯¹ä¸äºå¤§æ件çå¤çæ¯å¾ææçã
2 æ件åå ¥ææ¬æä½
æ件åå ¥æ两ç§åå ¥å½æ°åä¸ç§è¾ å©æ¯æã
write()ï¼åæ件ä¸åå ¥ä¸ä¸ªå符æè åèæµ
writelines()ï¼å°ä¸ä¸ªå ç´ å ¨ä¸ºå符串çå表åå ¥å°æä»¶ä¸ éè¦æ³¨æçæ¯ï¼writelinesåå ¥å表å ç´ çæ¶åä¼æå表å ç´ çå 容æ¼æ¥å°ä¸èµ·åå ¥ï¼ä¸ä¼ææ¢è¡åç©ºæ ¼ ã
seek()ï¼ è¾ å©åå ¥å½æ°offsetå移éåæ°ä»£è¡¨å«ä¹å¦ä¸
0 - æ件å¼å¤´
1 - å½åä½ç½®
2 - æ件ç»å°¾
2.1 æ¹æ³ä¸
file_name = input("output.txt", "w+")
text= "hello world!"file_name.write(text)
file.close()
2.2 æ¹æ³äº
file_name = input("output.txt", "w+")
list= ["ä¸å","æ©ä¸","æä¸"]
file_name.writelines(list)for line infile:
# 读ååå ¥çæ°æ®ï¼è¿æ¶ååç°æ¯æ²¡æä»»ä½å 容ç
file.close()
æ们å¢å ä¸è¡ä»£ç å°±å¯ä»¥è¯»åå°åå ¥çæ件å 容ï¼å©ç¨seek()å½æ°è°æ´åæä½æéçä½ç½®ï¼å¯ä»¥å®ç°åæä½ä¹åçæ£å¸¸è¯»åã
file_name = input("output.txt", "w+")
list= ["ä¸å","æ©ä¸","æä¸"]
file_name.readlines(list)
file_name.seek(0) # è°æ´åçæéå°æ件çå¼å§ä½ç½®for line infile:
# 读ååå ¥çæ°æ®ï¼è¿æ¶åä¼è¯»åºä¸è¡åå ¥çæ°æ®ã
file.close()
å¨ä¸å®åºæ¯ä¸æ们éè¦æææ¬å ¨é¨å 容读ååºæ¥ï¼è¿è¡å¤çãpythonæä¾ä¸ç§å½æ°è¯»åæ件ï¼åå«æ¯read readline readlinesï¼
read()ï¼è¯»åæ件çå ¨é¨å 容ï¼å ä¸åæ°å¯ä»¥æå®è¯»åçå符ã
readline()ï¼è¯»åæ件çä¸è¡ã
readlines()ï¼è¯»åæ件çææè¡å°å åä¸ã
ä¸ååºæ¯ä¸æ们å¯ä»¥éæ©ä¸åå½æ°å¯¹æ件è¿è¡è¯»åã
1.1 æ¹æ³ä¸
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")
txt=file.read()
# å ¨ææ¬çå¤ç
file.close()
使ç¨readå½æ°å°æ件ä¸çå å®¹å ¨é¨è¯»åï¼æ¾å¨å符串åétxtä¸ãè¿æ ·æä½éåäºææ¬è¾å°ï¼å¤çç®åçæ åµï¼å½æ件è¾å¤§æ¶ï¼è¿ç§æ¹å¼å¤çæ¶ä¸åéçãä¸æ¬¡æ§è¯»åè¾å¤§çæ件å°å åä¸ï¼ä¼èè´¹è¾å¤çæ¶é´åèµæºãè¿æ¶ååæ¹å¤çæææ´å¥½ã
1.2 æ¹æ³äº
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")
txt= file.read(4)
# ææ¬çå¤çwhile txt != ""txt= file.read(4)
# æ¹éææ¬å¤ç
file.close()
è¿ç§æ¹æ³éåäºåæ¹å¤çææ¬ä¿¡æ¯ï¼æ¯æ¬¡æ¹éè¯»å ¥ï¼æ¹éå¤çï¼ä¸ä¼å¯¹å åé æè¾å¤§çååã
1.3 æ¹æ³ä¸
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r")for line infile.readlines():
# å¤çæ¯ä¸è¡æ°æ®
file.close()
è¿ç§å¤çæ¹å¼éåå¤ç以è¡ä¸ºåå²ç¹ç¹çææ¬ï¼å¹¶ä¸ææ¬è¾å°ï¼å 为è¿ç§å¤çæ¹å¼éè¦ä¸æ¬¡æ§ææ件ææå 容读åå°å åä¸ã
1.4 æ¹æ³å
file_name = input("请è¾å ¥ä½ è¦æå¼çæ件çå®æ´è·¯å¾åå称")
file= open(file_name, "r") # è¿éçfileæ¶æ件å¥æfor line infile:
# å¤çæ¯ä¸è¡æ°æ®
file.close()
è¿ç§æ¹å¼åæ¹æ³ä¸ä¸çåºå«æ¯åè¡è¯»å ¥ï¼éè¡å¤çï¼ä¸ä¼ä¸æ¬¡æ§ææ件ææå 容é½è¯»å ¥å°å åä¸ï¼å¯¹ä¸äºå¤§æ件çå¤çæ¯å¾ææçã
2 æ件åå ¥ææ¬æä½
æ件åå ¥æ两ç§åå ¥å½æ°åä¸ç§è¾ å©æ¯æã
write()ï¼åæ件ä¸åå ¥ä¸ä¸ªå符æè åèæµ
writelines()ï¼å°ä¸ä¸ªå ç´ å ¨ä¸ºå符串çå表åå ¥å°æä»¶ä¸ éè¦æ³¨æçæ¯ï¼writelinesåå ¥å表å ç´ çæ¶åä¼æå表å ç´ çå 容æ¼æ¥å°ä¸èµ·åå ¥ï¼ä¸ä¼ææ¢è¡åç©ºæ ¼ ã
seek()ï¼ è¾ å©åå ¥å½æ°offsetå移éåæ°ä»£è¡¨å«ä¹å¦ä¸
0 - æ件å¼å¤´
1 - å½åä½ç½®
2 - æ件ç»å°¾
2.1 æ¹æ³ä¸
file_name = input("output.txt", "w+")
text= "hello world!"file_name.write(text)
file.close()
2.2 æ¹æ³äº
file_name = input("output.txt", "w+")
list= ["ä¸å","æ©ä¸","æä¸"]
file_name.writelines(list)for line infile:
# 读ååå ¥çæ°æ®ï¼è¿æ¶ååç°æ¯æ²¡æä»»ä½å 容ç
file.close()
æ们å¢å ä¸è¡ä»£ç å°±å¯ä»¥è¯»åå°åå ¥çæ件å 容ï¼å©ç¨seek()å½æ°è°æ´åæä½æéçä½ç½®ï¼å¯ä»¥å®ç°åæä½ä¹åçæ£å¸¸è¯»åã
file_name = input("output.txt", "w+")
list= ["ä¸å","æ©ä¸","æä¸"]
file_name.readlines(list)
file_name.seek(0) # è°æ´åçæéå°æ件çå¼å§ä½ç½®for line infile:
# 读ååå ¥çæ°æ®ï¼è¿æ¶åä¼è¯»åºä¸è¡åå ¥çæ°æ®ã
file.close()
温馨提示:答案为网友推荐,仅供参考