Python Read and write files

Source: Internet
Author: User


# python read-write files


# # code
-----------------------------
"' Python
#! /usr/bin/python
Import Os,sys

Try
Fsock = open ("d:/svntest/test.py", "R")
Except IOError:
Print "The file don t exist, please double check!"
Exit ()
print ' The file mode is ', Fsock.mode
print ' The file name is ', Fsock.name
P = Fsock.tell ()
print ' The postion is%d '% (P)
Fsock.close ()

#Read file
Fsock = open ("d:/svntest/test.py", "R")
Alllines = Fsock.readlines ()
#Method 1
For Eachline in Fsock:
Print Eachline

#Method 2
print ' Star ' + ' = ' *30
For Eachline in Alllines:
Print Eachline
print ' End ' + ' = ' *30
Fsock.close ()

#write This file
Fsock = open ("d:/svntest/test.py", "a")
Fsock.write ("" "
#Line 1 Just for test purpose
#Line 2 Just for test purpose
#Line 3 Just for test Purpose "" ")
Fsock.close ()


#check The file status
S1 = fsock.closed
if True = = S1:
print ' The file is closed '
Else
print ' The file donot close '
```

# # References
-----------------------------
-[Rookie tutorial] (http://www.runoob.com/python/file-methods.html)

Python file (file) method

The file object is created using the Open function, and the following table lists the functions commonly used by file objects:

Serial Number Method and Description
1

File.close ()

Close the file. The file can no longer read and write after closing.

2

File.flush ()

Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.

3

File.fileno ()

Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module.

4

File.isatty ()

Returns True if the file is connected to an end device, otherwise False is returned.

5

File.next ()

Returns the next line of the file.

6

File.read ([size])

Reads the specified number of bytes from the file, if none is given or is negative.

7

File.readline ([size])

Reads the entire line, including the "\ n" character.

8

File.readlines ([Sizehint])

Reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.

9

File.seek (offset[, whence])

Set the current location of the file

10

File.tell ()

Returns the current location of the file.

11

File.truncate ([size])

Intercepts the file, the truncated byte is specified by size, and the default is the current file location.

12

File.write (str)

Writes a string to the file without a return value.

13

File.writelines (Sequence)

Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.

Python Read and write 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.