File I/O in Python

Source: Internet
Author: User

Read keyboard input: Raw_input input

The Raw_input ([prompt]) function reads a line from the standard input and returns a string (minus the end of the newline character):

str = raw_input("Please enter:");

Print "What you have entered is:", str

The input ([prompt]) function is basically similar to the raw_input ([prompt]) function, but input can receive a Python expression as input and return the result of the operation.

= input("Please enter:");  Print"What you have entered is:", str       



Fo=Open ( "foo.txt" ,  "WB" print  "file name:"  ,  Fo.print  "closed:" , fo closedprint  "access mode:" ,< Span class= "PLN" > Fo.print  "at the end of force plus spaces:"  Fo.                

When a reference to a file object is re-assigned to another file, Python closes the previous file. Closing a file with the close () method is a good habit.

Grammar:

FileObject.  Close();  

Write () method

The write () method writes any string to an open file. It is important to note that the Python string can be binary data, not just text.

The Write () method does not add a line break (' \ n ') at the end of the string:

# Opens a file  = Open("Foo.txt","WB")fo.  Write("Www.runoob.com!\nvery good site!\n");  # Close the Open file fo.  Close()                 

The above method creates a Foo.txt file, writes the received content to the file, and eventually closes the file. If you open this file, you will see the following:

$ cat foo.  TXT www.  Runoob.  COM!  Very Good site        


FileObject.  Read([Count]);    

Here, the parameter being passed is the count of bytes to read from the open file. The method reads in from the beginning of the file, and if it does not pass in count, it tries to read as much more content as possible, most likely until the end of the file.

File Location:

The tell () method tells you the current position within the file, in other words, the next read and write occurs after so many bytes at the beginning of the file.

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.

If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.

#!/usr/bin/python#-*-Coding:utf-8-*- # Open a fileFo=Open("Foo.txt", "R+")Str=Fo.Read(10);Print "The string read is:",Str# Find the current locationPosition=Fo.Tell();Print  "Current file location:" , position # Reposition the pointer again to the beginning of the file position = Fo. Seek (0, 0 str = Fo.10print  "reread string:" , str< Span class= "com" ># close open file fo. ()               

Rename:os. Rename(current_file_name, new_file_name)

For example:
import os
# rename files test1.txt to Test2.txt. os.  Rename("Test1.txt","Test2.txt")       
Remove () method

You can delete the file using the Remove () method, and you need to provide the filename to delete as a parameter.

Grammar:

Os.  Remove(file_name)    


mkdir () method

You can use the mkdir () method of the OS module to create new catalogs in the current directory. You need to provide a parameter that contains the name of the directory you want to create.

Grammar:

Os.  mkdir("Newdir")    


ChDir () method

You can use the 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.

Grammar:

Os.  ChDir("Newdir")    

Example:

The following example goes to the "/home/newdir" directory.

#!/usr/bin/python#-*-coding:utf-8-*-Import# Changes the current directory to "/home/newdir"os.  ChDir("/home/newdir")        


GETCWD () Method:

The GETCWD () method displays the current working directory.

Syntax:os. GETCWD()

Import# gives the current directory print os.  GETCWD()     
RmDir () method

The RmDir () method deletes the directory, and the directory name is passed as a parameter.

Before deleting this directory, all of its contents should be purged first.

Grammar:

Os.  RmDir(' dirname ')    

File I/O in Python

Related Article

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.