日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python文件操作模式是什么,python --文件操作模式详解

發(fā)布時(shí)間:2024/9/27 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python文件操作模式是什么,python --文件操作模式详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#f= open(r"aaa/a.txt",mode='rt') # f的值是一種變量,占用的是應(yīng)用程序的內(nèi)存空間,此時(shí)牽扯的是兩個(gè)方面的資源

#print(f)

##2.操作文件:讀/寫文件,應(yīng)用程序?qū)ξ募淖x寫請求都是向操作系統(tǒng)調(diào)用,然后由操作系統(tǒng)控制把硬盤把輸入讀入內(nèi)存,或者寫入硬盤

#res=f.read()

#print(res)

#關(guān)閉文件 z

#f.close()"""回收操作系統(tǒng)資源,f 還存在"""

#f變量還存在,但是不能再讀了

#del f """回收應(yīng)用程序程序資源"""

#文件對象又稱為文件句柄

# with open("a.txt",mode='rt') as f1:

# res=f1.read()

# print(res)

# with open("a.txt",mode='rt') as f1,open("b.txt",mode='rt') as f2:

# res1=f1.read()

# res2=f2.read()

# print(res1)

# print(res2)

"""指定字符編碼"""

"""t 文本(默認(rèn)的模式)

1。 讀寫都是以str (unicode)為單位的

2。文本文件

3。必須指定encoding='utf-8'"""

# 沒有指定encoding 參數(shù)操作系統(tǒng)會(huì)使用自己的默認(rèn)的編碼,

# Linux 和mac 默認(rèn)使用utf-8

# Windows默認(rèn)gbk

# with open ('c.txt',mode='rt',encoding='utf-8') as f:

# res=f.read() #t模式會(huì)將f.read()讀出來的結(jié)果解碼成unicode

# print(res,type(res))

#

#內(nèi)存:utf-8格式的二進(jìn)制----解碼--->unicode

#硬盤(c.txt 內(nèi)容 utf-8格式的二進(jìn)制)

#以t 模式為基礎(chǔ)進(jìn)行內(nèi)存操作

##1.rt

在本python文件的目錄下創(chuàng)建一個(gè)name.txt的文本文件,

izhan:1005

inp_username = input("please input your name:").strip()

inp_password = input("please input your password:").strip()

with open("name.txt",mode='rt',encoding='utf-8') as f:

res=f.read()

print(res)

username,password=res.split(":")

print(username)

print(password)

print(inp_username)

print(inp_password)

if inp_username == username and inp_password == password:

print("congratulations! you can login")

else:

print("sorry! the password is wrong or has no this user")

please input your name:izhan

please input your password:1005

izhan

1005

izhan

1005

congratulations! you can login

隨便輸入看看:

please input your name:dandan

please input your password:0711

izhan

1005

dandan

0711

sorry! the password is wrong or has no this user

Process finished with exit code 0

再創(chuàng)建一個(gè)name.txt 文件

izhan:1005

lili:1111

dandan:0711

另外:python spit 的學(xué)習(xí):

https://www.cnblogs.com/clairedandan/p/10926173.html

inp_username = input("please input your name:").strip()

inp_password = input("please input your password:").strip()

with open("name.txt",mode='rt',encoding='utf-8') as f:

for line in f:

print(line,end='') # 沒有end='',結(jié)果就會(huì)一行一行中間有一行空的。

username,password=line.strip().split(":")

# print(username)

# print(password)

if inp_username == username and inp_password == password:

print("login successfully")

break

else:

print("賬號(hào)錯(cuò)誤")

please input your name:lili

please input your password:0711

izhan:1005

lili:1111

dandan:0711賬號(hào)錯(cuò)誤

Process finished with exit code 0

please input your name:izhan

please input your password:1005

izhan:1005

login successfully

Process finished with exit code 0

"""w:只寫模式,當(dāng)文件不存在時(shí)會(huì)創(chuàng)造空文件,當(dāng)文件存在時(shí)會(huì)清空文件,"""

# with open("d.txt",mode="wt",encoding="utf-8") as f:

# # f.read()"""報(bào)錯(cuò),不可讀"""

# f.write("hahaha\n")

# f.write("hello\n")

# f.write("wwwwww\n")

##如果重新打開文件,則會(huì)清空之前的內(nèi)容,如果連續(xù)write,則會(huì)連續(xù)寫的

"""3.a 只追加寫,"""

# with open("a.txt",mode="at",encoding="utf-8") as f:

# # f.read() 不能讀

# f.write("hahahahah\n")

# f.write("enenenenen\n")

# f.write("oooooo\n")

"""a 重新打開文件,不會(huì)清空文件內(nèi)容,會(huì)將文件指針直接移動(dòng)到末尾

a 文件一直用作記錄日志,注冊功能"""

"""a 模式注冊功能"""

# name=input("please input your name:")

# pwd=input("please input your password:")

# with open("db.txt",mode="at",encoding="utf-8") as f:

# f.write("{}:{}\n".format(name,pwd))

w 可以用做文本文件的copy

with open("e.txt",mode="rt",encoding="utf=8")as f1,\

open("f.txt",mode="wt",encoding="utf-8")as f2:

res=f1.read()

f2.write(res)

src_file=input("源文件文件:")

dst_file=input("目標(biāo)文件:")

with open(r"{}".format(src_file),mode="rt",encoding="utf=8")as f1,\

open(r"{}".format(dst_file),mode="wt",encoding="utf-8")as f2:

res=f1.read()

f2.write(res)

/usr/local/bin/python3.8 /Users/futantan/PycharmProjects/S14/文件知識(shí)/文件處理.py

源文件文件:/Users/futantan/PycharmProjects/S14/文件知識(shí)/a.txt

目標(biāo)文件:/Users/futantan/PycharmProjects/S14/a_copy.txt

Process finished with exit code 0

此時(shí)就可以發(fā)現(xiàn)有一個(gè)copy的文件了

標(biāo)簽:txt,python,res,--,詳解,mode,print,input,password

來源: https://www.cnblogs.com/clairedandan/p/14105609.html

總結(jié)

以上是生活随笔為你收集整理的python文件操作模式是什么,python --文件操作模式详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。