日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

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

#print(f)

##2.操作文件:讀/寫文件,應用程序對文件的讀寫請求都是向操作系統調用,然后由操作系統控制把硬盤把輸入讀入內存,或者寫入硬盤

#res=f.read()

#print(res)

#關閉文件 z

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

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

#del f """回收應用程序程序資源"""

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

# 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 文本(默認的模式)

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

2。文本文件

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

# 沒有指定encoding 參數操作系統會使用自己的默認的編碼,

# Linux 和mac 默認使用utf-8

# Windows默認gbk

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

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

# print(res,type(res))

#

#內存:utf-8格式的二進制----解碼--->unicode

#硬盤(c.txt 內容 utf-8格式的二進制)

#以t 模式為基礎進行內存操作

##1.rt

在本python文件的目錄下創建一個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

再創建一個name.txt 文件

izhan:1005

lili:1111

dandan:0711

另外:python spit 的學習:

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='',結果就會一行一行中間有一行空的。

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

# print(username)

# print(password)

if inp_username == username and inp_password == password:

print("login successfully")

break

else:

print("賬號錯誤")

please input your name:lili

please input your password:0711

izhan:1005

lili:1111

dandan:0711賬號錯誤

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:只寫模式,當文件不存在時會創造空文件,當文件存在時會清空文件,"""

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

# # f.read()"""報錯,不可讀"""

# f.write("hahaha\n")

# f.write("hello\n")

# f.write("wwwwww\n")

##如果重新打開文件,則會清空之前的內容,如果連續write,則會連續寫的

"""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 重新打開文件,不會清空文件內容,會將文件指針直接移動到末尾

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("目標文件:")

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/文件知識/文件處理.py

源文件文件:/Users/futantan/PycharmProjects/S14/文件知識/a.txt

目標文件:/Users/futantan/PycharmProjects/S14/a_copy.txt

Process finished with exit code 0

此時就可以發現有一個copy的文件了

標簽:txt,python,res,--,詳解,mode,print,input,password

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。