Python file processing

Source: Internet
Author: User

1 What is a file?
A file is a virtual unit of a read-write drive provided by the operating system for a user or application
The operating core of the file is: Read, write
That is, we only need to read and write operations, that is, the operating system initiated the request, and then by the operating system to the user or application
Read and write to the file into specific hard disk instructions (such as control disc rotation, control the mechanical arm movement to read and write data)

2 Why should I have a file?
Because memory is not able to persist data permanently, we need to save the data to the hard drive whenever we want to.
And the operation of the file can be implemented on the hardware

1. Open File

F=open (R ' E:\SH_fullstack_s2\day08\a.txt ', mode= ' R ') # initiates a request to the operating system, requires the operating system to open the file, consumes the operating system's resources


2. Reading and writing

Data=f.read () # Opens an originating read request to the operating system, which is transferred by the operating system to a specific hard disk operation and reads the contents from the hard disk into memory
Print (data)

3. Close

F.close () #向操作系统发起请求, requires the operating system to shut down files, recycle system resources

There are two ways to control the contents of a read-write file: T-mode text,b Model bytes
Attention:
1, T and b these two modes can not be used alone, all need to use with one of the r/w/a
2, the default content format is T
3, only text files can be used in T mode, and only text files have the concept of character encoding

# There are three basic modes of manipulating files:
#1, R, default
#2, W
#3, a

R:read, read-only mode
1, can only read, can not write
2, when the file does not exist, will error, when the file exists, the file pointer will be moved to the beginning
F=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
Data1=f.read ()
Print (' First time: ', data1)
Print (Type (data1))

#data2 =f.read ()
#print (' Second time: ', data2)
F.close ()

F=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
Data2=f.read ()
Print (' Second time: ', data2)
F.close ()

f=open (R ' b.txt ', mode= ' RT ', encoding= ' utf-8 ')
print (f.readable ())
print (F.readline (), end= ')
print ( F.readline (), end= ')

lines=f.readlines ()
print (lines)
f.close ()

#w: write-only mode
#1, write-only, unreadable
#2, empty files are created when a file does not exist, the contents of the file are emptied when the file exists
# F=open (R ' C.txt ', mode= ' wt ', encoding= ' utf-8 ')
# # Print (f.readable ())
Span style= "font-size:14pt" ># # f.read ()
# # f.write (' Hello my god, hello, my land. n Ah ah ah ah \ n ')
# # f.write (' 4444\n ')
# # f.write (' 5555 ')
#
# lines=[' 1111\n ', ' 2222\n ', ' 33333\n ']
# # for line in lines:
# # F.write (line)
# f.writelines (lines)
#
Span style= "font-size:14pt" ># f.close ()


#a: Append write mode only
#1, can only write, cannot read
#2, creates an empty file when the file does not exist, moves the pointer to the end of the file when the file exists

# f=open (' D.txt ', mode= ' at ', encoding= ' utf-8 ')
# Print (f.readable ())
# F.read ()

# f.write (' 555555\n6666\n ')
# f.writelines ([' 77777\n ', ' 8888888\n ', ' 999999999\n '])

# F.close ()

#b: Binary mode
#注意:
#1, must not specify character encoding, only the T mode is related to character encoding
#2, B is a binary mode, is a common file read mode, because all the files in the hard disk are stored in binary form

# f=open (' egon.jpg ', ' RB ')
# Data=f.read ()
# Print (Type (data))
# F.close ()

# f=open (' Oldsiji.mp4 ', ' RB ')
# Data=f.read ()
# F.close ()

# F=open (R ' F:\oldsiji.mp4 ', ' WB ')
# f.write (data)
# F.close ()


# cyclic Reading
# f=open (' d.txt ', ' RT ', encoding= ' utf-8 ')
# for line in F:
# Print (line)
# F.close ()


# f=open (' D.txt ', ' RB ')
# for line in F:
# Print (line)
# F.close ()

# f=open (' Oldsiji.mp4 ', ' RB ')
# for line in F:
# Print (line)
# F.close ()


# f=open (' E.txt ', ' WB ')
# f.write (' Hello ', encode (' Utf-8 '))
#
# F.close ()


# f=open (' e.txt ', ' AB ')
# f.write (' Hello, I'm Egon '. Encode (' Utf-8 '))
# F.close ()


# Context Management
# with open (' e.txt ', ' RB ') as F,open (' j.txt ', ' wt ', encoding= ' utf-8 ') as F1:
# #文件的操作
# Src_data=f.read ()
# res=src_data.decode (' Utf-8 ')
# # Print (Res,type (res))
# F1.write (RES)

With open (' e.txt ', ' RB ') as F,\
Open (' J.txt ', ' WB ') as F1:
#文件的操作
F1.write (F.read ())

Python file processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.