Python Basic 10-I/O programming

Source: Internet
Author: User

Tag: Buffer file copy src file operation off Stat Ali

I. INPUT and OUTPUT

① input: Input ()

② output: Print ()

1 name=input (' Please enter name ')2print(name)

Second, the file operation

① Open File: File object =open (file name, access mode, buffering)

FileName: Specifies the file to be opened, usually including a path, either an absolute or a relative path

Buffering: Optional parameters, specify the buffer mode used by the file, buffering=0, do not buffer; buffering=1, buffer one line; buffering>1, use the given value as the buffer size

Access mode: Used to specify the mode of open file, the parameter table of access mode is as follows

desirable value
r read-open
w< /td> opens in a written manner, when the contents of the file are emptied before the file is created
a
r+ , w+ Open in read-write mode
a+ append read-write mode opens
RB Open in binary read mode
WB open in binary write mode
rb+, wb+, ab+ open in binary read-write mode

② Close file: File object. Close ()

③ Read file:

Ⅰ:str= file object. read ([b])

Optional parameter [b]: Specify the number of bytes read, default Read all

The ⅱ:list= file object. ReadLines () reads the contents back into the list of strings

The ⅲ:str= file object. ReadLine () reads all rows from a file at once

Ⅳ: Using the IN keyword: for lines in file object: processing row data line

1F=open ('Test.txt')2Mystr=F.read ()3 f.close ()4 Print(MYSTR)5 6F=open ('Test.txt')7  whileTrue:8Chunk=f.read (10)9     if  notChunk:Ten          Break One     Print(Chunk) AF.close
1F=open ('Test.txt')2mylist=F.readlines ()3 Print(mylist)4 f.close ()5F=open ('Test.txt')6  whileTrue:7chunk=F.readline ()8     if  notChunk:9          BreakTen     Print(Chunk) OneF.close ()

Third, write the file

①write (): File object. write (write content)

② Append write: When opening a file, you can call open () with a or a + as a parameter and then write

③writelines (): File object. (seq) for writing string sequences, seq is a sequence of returned strings (lists, tuples, collections, etc.)

1F=open ('Test.txt','A +')2F.write ('Hello')3 f.close ()4F=open ('Test.txt')5  forLineinchF:6     Print(line)7F.close ()
1mylist=['Hello, Jack .','Hello Salar','Hello Coffee']2F=open ('Test.txt','A +')3 f.writelines (mylist)4 f.close ()5F=open ('Test.txt')6  forLineinchF:7     Print(line)8F.close ()

Iv. File Pointers

① get the file pointer location: pos= file object. Tell () returns an integer that represents the location of the file pointer, the position of the file pointer when a file is opened, 0, and a read-write file where the position of the file pointer moves to the read-write location

② Moving File Pointers: file objects. Seek ((Offset,where))

Offset: The shift in bytes, a positive number moves toward the end of the file, and a negative amount moves toward the head of the file

Where: Refers to where to start moving, 0 is the starting position, equal to 1 o'clock moves for the current position, equal to 2 o'clock starting from the end position to move

1 f=open ('test.txt')2print(F.tell ()) 3 f.close () 4 f=open ('test.txt')5 f.seek (5, 0) 6 Print (F.tell ()) 7 F.close ()

V. Truncation of files

File object. truncate ([size]) truncates the file from the beginning of the file, size is an optional parameter, and the contents of the file after the size byte are truncated and discarded

1F=open ('Test.txt','A +')2F.truncate (5)3 f.close ()4F=open ('Test.txt')5  forLineinchF:6     Print(line)7F.close ()

Vi. Other operations

① file properties: Use the stat () function of the OS module to get the properties of a file, create a time, modify time, access time, file size, and other attributes (to import a module before using: Imported OS)

② Copying files: Using the copy () function of the Shutil module, the function prototype is: Copy (SRC,DST) to copy the source file src to DST

③ moving files: Using the Move () function of the Shutil module, the function prototype is: Move (SRC,DST)

④ renaming files: Using the rename () function of the OS module, the function prototype is: Os.rename (original filename, new file name)

⑤ deleting files: Using the Remove () function of the OS module, the function prototype is: Os.remove (SRC)

1 ImportOS2 ImportShutil3Filestats=os.stat ('Test.txt')4 Print(filestats)5 6Shutil.copy ('F:\\python\\book\\ch7\\test.txt','F:\\python\\book\\ch7\\test2.txt')7Shutil.move ('F:\\python\\book\\ch7\\test2.txt','F:\\python\\book\\test2.txt')8Os.rename ('F:\\python\\book\\test2.txt','F:\\python\\book\\test3.txt')9Os.remove ('F:\\python\\book\\test3.txt')

Seven, directory programming

① get current directory: OS module's GETCWD () function

② Get directory Contents: OS module's Listdir () function

③ Creating a directory: the mkdir () function of the OS module

④ Delete directory: the RmDir () function of the OS module

1 Import OS 2 Print (OS.GETCWD ()) 3 Print (Os.listdir (OS.GETCWD ())) 4 os.mkdir ('f:\\python\\book\ch7\\he')5 Os.rmdir ('f:\\python\\book\ch7\\he')

Python Basic 10-I/O programming

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.