Python Note five: io and files

Source: Internet
Author: User

1.python IO:
Python provides two built-in functions to read a line of text from standard input, and the default standard input is the keyboard:
1) The Raw_input ([prompt]) function reads a line from the standard input and returns a string
2) input ([prompt]) function assumes that your input is a valid Python expression and returns the result of the operation.
3) Eg:
Str=raw_input ("Enter something:") #输入的格式可以是任何形式
Print str
Str=input ("Enter something:") #输入的格式必须是有效的python表达式
Print str

2. Basic operation: (File module)

1) Open () function: A file object should be created first when opening the files:
File Object = open (file_name [, access_mode][, buffering]) parameter description:
A) The File_name:file_name variable is a string value that contains the name of the file you want to access.
b) Access_mode:access_mode determines the mode of open files: read-only, write, append, etc. All the desirable values are shown in the full list below. This parameter is non-mandatory and the default file access mode is read-only (R).
c) Buffering: If the value of buffering is set to 0, there will be no deposit. If the value of buffering is 1, the row is stored when the file is accessed. If you set the value of buffering to an integer greater than 1, it indicates that this is the buffer size of the storage area. If a negative value is taken, the buffer size of the storage area is the system default.

   file operation mode:
R opens the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W opens a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
WB opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to
AB opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. The new content will be written to the existing content. If the file does not exist, create a new file to write to
A + opens a file for read and write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.

2) A list of all properties related to the file object:
File.closed returns True if the file has been closed, otherwise false is returned.
File.mode returns the access mode of the file being opened.
File.name returns the name of the file.
File.softspace if the print output must be followed by a space character, false is returned. Otherwise, returns True.

3) The close () method flushes any information that has not yet been written in the buffer and closes the file, which can no longer be written.
Fileobject.close ();

4) The Write () method writes any string to an open file. It is important to note that the Python string can be binary, not just text. Write () does not add a line break (' \ n ') at the end of the string:
Fileobject.write (string);
5) The Read () method reads a string from an open file. It is important to note that the Python string can be binary data, not just text.
Fileobject.read ([count]);
6) The Tell () method tells you the current position within the file, in other words, the next read and write will occur after so many bytes at the beginning of the file
7) The Seek (offset [, from]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.
3. Renaming and deleting files (OS module)
1) The rename () method requires two parameters, the current file name, and a new filename.
Os.rename (Current_file_name, New_file_name)
2) The Remove () method deletes the file and needs to provide the filename to delete as the parameter.
Os.remove (file_name)
3) The MkDir () method creates a new directory under the current directory. You need to provide a parameter that contains the name of the directory you want to create.
Os.mkdir ("Newdir")
4) ChDir () method to change the current directory. The ChDir () method requires a parameter that you want to set as the directory name of the current directory.
Os.chdir ("Newdir")
5) The RmDir () method deletes the directory, the directory name is passed as a parameter, and all its contents should be purged before the directory is deleted.
Os.rmdir (' dirname ')

Python Note five: io and files

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.