Read the file python basis of

# Read the file name txt file 

reading open # files ( "file", "literacy method") with open ( "file", "literacy method") as a handle to:
# create a file or write to file in
# = Open File ( "userinfo.txt", "w", encoding = "UTF-8")
# file.write ( "life is short, \ n nothing much money, \ n carpe diem.")
# how to determine whether the file is closed it? file.closed returns True if closed, the anti-False
# Print ( "Check that file is normally closed:", file.closed)
# close the file
# File.close ()
# Print ( "Check that file is normally closed : ", file.closed)

# r + represents a readable and writable file exists on the cover
# = Open file (" userinfo.txt "," r + ", encoding =" UTF-8 ")
# file.write (" China you good ")
to close the file #
# File.close ()


# read the file contents
#read (bytes) read all content or a byte
# file = open (" userinfo.txt " ," r ", encoding = "



Print # (FILE.readline ())
#radelines read all of the content, and all contents stored in a list, line breaks \ n
# Print (file.readlines ())

# remove \ n
converted into a string list # "" .join (list) -> str.strip ( ) to filter out special characters
# n1 = file.readlines () # list
# n2 = "" .join (N1) #
# # Print (n2.strip ())
# # Print (of the type (n2.strip ()))

# append files & a + a
# = Open file ( "userinfo.txt", "a", encoding = "UTF-8")
# file.write ( "the Hello, World ")
Close file #
# File.close ()


# R & lt read a file, the file does not exist on the error read, r + can read and write files, overwrite the original file content
# file = open (" testinfo " ," r ", = encoding "UTF-. 8")
# Print (file.readlines ())
#FileNotFoundError: [Errno 2] No SUCH File or Directory: 'testinfo'

# W write the contents of the file, remove the original file contents; w + content to read and write files, clear the contents of the original file
Open File = # ( "userinfo.txt", "w +", encoding = "UTF-8")
# file.write ( "Life is short, \ n nothing much money, \ n carpe diem .hello")
# closing the file
# File.close ()


# managed with open as the context

# with Open ( "userinfo.txt", "W", encoding = "UTF-. 8") AS F:
# f.write ( "contexts write the contents ")

# with Open (" userinfo.txt "," R & lt ", encoding =" UTF-. 8 ") AS F:
# Print (f.readlines ())
# Print (" check whether the file is normally closed : ", f.closed)


# write class test scores, recorded in score.txt
" ""
Xiao Ming, 55
flowers, 56
Xiaohua, 77
Wang, 84
Xiao Liu, 65
small study,
"" "
Import Re
# calculate score.txt file for all test scores average
# write the contents to score.txt 文件中
# with open("score.txt","w",encoding="utf-8") as file:
# A file.write ( "Bob, 55 \ n florets, 56 \ n Xiaohua, 77 \ n Wang, 84 \ n Liu, 65 \ n small learning")
# read content, calculates an average value
with open ( "score.txt", "r", encoding = "UTF-8") AS File:
List = file.readlines ()
str1 = "" .join (List)
str2 = str1.strip ()
# Print (str2, of the type (str2))
List1 = re.findall ( ", (. +)", str2) # use regular expressions to extract numbers, returns a list of
# Print (List1, of the type (List1))
NUM = 0
for index in the Range ( len (the List1)):
# Print (index)
NUM + = int (the List1 [index])
Print (NUM / len (the List1))

Guess you like

Origin www.cnblogs.com/Teachertao/p/11707840.html