鄭紹麒python 檔案方法 UTF-8

編碼方法的使用情況 UTF-8

截圖

代碼

f = open("ascii.txt", "r+",encoding='utf-8')
#註解開啟open檔案ascii.txt讀取模式r開啟之後要關閉
x = f.read()
f.close()
print("檔案長度", len(x))
print("列印內容",x)
f = open("ascii.txt", "r")
y = f.readlines() #輸出成串列list,長度是列數
print(y)
f.write('鄭紹麒')
f.close

ascii art字元藝術

w3schools Python File Methods

MethodDescription
close()Closes the file
detach()Returns the separated raw stream from the buffer
fileno()Returns a number that represents the stream, from the operating system's perspective
flush()Flushes the internal buffer
isatty()Returns whether the file stream is interactive or not
read()Returns the file content
readable()Returns whether the file stream can be read or not
readline()Returns one line from the file
readlines()Returns a list of lines from the file
seek()Change the file position
seekable()Returns whether the file allows us to change the file position
tell()Returns the current file position
truncate()Resizes the file to a specified size
writable()Returns whether the file can be written to or not
write()Writes the specified string to the file
writelines()Writes a list of strings to the file

留言

這個網誌中的熱門文章

鄭紹麒VScode, HTML檔案, CSS檔案, JS檔案

鄭紹麒數學方程式排版練習