File operation

Contents

  • File operations
    • What is a file?
    • Why are there files?
    • The process of opening files
    • How does python operate files

< /div>

File operation

What is a file?

The so-called “file” is a unit defined in our computer for the purpose of realizing a certain function or part of a certain software function.

Why do we have files

The memory cannot store data permanently, but whenever we want to store data permanently, we need to save the file to the hard disk, and the operation The file can realize the operation on the hardware.

File opening process

  • Step1 Click the file to be opened
  • Step2 The operating system receives the command, The file path is sent to the CPU
  • step3 The CPU searches for the file in the hard disk according to the path, and then retrieves it into the memory
  • Step4 The operating system displays the retrieved file, which can be read and written Operations such as saving

How to manipulate files in python

Now I have a string that needs to be stored in a file, how to do it through programming Realize it?

str1 ='Bonnie is a beautiful girl'

Read data from the hard disk

In python There is a open(file,mode,encoding) function to open a specific file

  • file represents the file path
  • mode represents the file operation Mode, the default is read-only and not write
  • encoding means the file decoding method
open(r'D:\Python programming file\Programming Myself\ Test file')
<_io.TextIOWrapper name='D:\\Python programming file\\Programming Myself\\Test file' mode='r' encoding='cp936'>

< p>How to open the file

  • read mode
f = open(r'D:\ Python programming file\program Myself\test file',mode='r',encoding='utf-8')print(f.read())f.close()
Bonnie is a beautiful girl 
  • write mode
fw = open(r'D:\Python programming file\Programming Myself\test file',mode= 'w',encoding='utf-8')fw.write('hades is handsome')fw.flush()fw.close() # Replace "Bonnie is a beautiful girl" with hades is handsome''' The final print result: hades is handsome'''
  • append mode
fa = open(r'D:\Python programming file \Programming Myself\Test file',mode='a',encodi ng='utf-8')fa.write('\nBonnie is a beautiful girl')fa.flush()fa.close()'''Final print result: hades is handsome
Bonnie is a beautiful girl '''

Absolute path and relative path

  • The absolute path starts from the root directory, such as open(r'D: \Python programming file\program Myself\test file')
  • The relative path means that the files are all in the same folder, such as open(r'test file')
  • code>

with manage file operation context

  • Implement file opening
with open(r'D:\Python programming file\Programming Myself\test file','r', encoding='utf8') as fr: data = fr.read() print(data) 
hades is handsome
Bonnie is a beautiful girl
  • Realize file write
with open(r 'D:\Python programming file\Programming Myself\test file','w', encoding='utf8') as f: f.write('hades is handsome')with open(r'D:\Python programming file\ Programming Myself\test file','r', encoding='utf8') as fr: data = fr.read() print(data)
hades is handsome
with open(r'D:\Python programming file\Programming Myself\test file','a', encoding='utf8') as f: f.write('\nBonnie is a beautiful gi rl')with open(r'D:\Python programming file\Programming Myself\test file','r', encoding='utf8') as fr: data = fr.read() print(data)
hades is handsome
Bonnie is a beautiful girl
  • Realize file copy
with open('test ','r', encoding='utf8') as fr, open('test1','w', encoding='utf8') as fw: data = fr.read() fw.write(data)

Directory

  • File operation
    • What is a file ?
    • Why are there files?
    • The process of opening files
    • How does python operate files

< /div>

  • File operation
    • What is a file?
    • Why is there a file
    • The process of opening the file
    • How does python operate the file

< /p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1355 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.