python-basic-file operations

1 Opening and closing of files

Open

In python, using the open function, you can open an existing file or create a new file

open(filename, access mode)

An example is as follows:


    f = open('test.txt', 'w')

illustrate:

access mode illustrate
r Open the file as read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
w

Open a file for writing only.

Overwrite the file if it already exists. If the file does not exist, create a new file.

a

Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file.

That is, the new content will be written after the existing content. If the file does not exist, create a new file for writing.

rb Open 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.
wb

Open 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.

away

Open a file in binary format for appending.

If the file already exists, the file pointer will be placed at the end of the file.

That is, the new content will be written after the existing content. If the file does not exist, create a new file for writing.

r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
w+

Open a file for reading and writing.

Overwrite the file if it already exists. If the file does not exist, create a new file.

a+

Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file.

The file will be opened in append mode. If the file does not exist, create a new file for reading and writing.

rb+ Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file.
wb+

Open a file in binary format for reading and writing.

Overwrite the file if it already exists. If the file does not exist, create a new file.

ab+

Open 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 for reading and writing.

close file

close( )

An example is as follows:

# Create a new file with the file name: test.txt
    f = open('test.txt', 'w')

    # close this file
    f.close()

 

2 Reading and writing of files

 

 

 

 3 Positioning and reading and writing of files

<1> Get the current read and write position

In the process of reading and writing files, if you want to know the current position, you can use tell() to get

    # 打开一个已经存在的文件
    f = open("test.txt", "r")
    str = f.read(3)
    print "读取的数据是 : ", str

    # 查找当前位置
    position = f.tell()
    print "当前文件位置 : ", position

    str = f.read(3)
    print "读取的数据是 : ", str

    # 查找当前位置
    position = f.tell()
    print "当前文件位置 : ", position

    f.close()

 

<2>定位到某个位置

如果在读写文件的过程中,需要从另外一个位置进行操作的话,可以使用seek()

seek(offset, from)有2个参数

  • offset:偏移量
  • from:方向
    • 0:表示文件开头
    • 1:表示当前位置
    • 2:表示文件末尾

demo:把位置设置为:从文件开头,偏移5个字节

    # 打开一个已经存在的文件
    f = open("test.txt", "r")
    str = f.read(30)
    print "读取的数据是 : ", str

    # 查找当前位置
    position = f.tell()
    print "当前文件位置 : ", position

    # 重新设置位置
    f.seek(5,0)

    # 查找当前位置
    position = f.tell()
    print "当前文件位置 : ", position

    f.close()

 

demo:把位置设置为:离文件末尾,3字节处

    # 打开一个已经存在的文件
    f = open("test.txt", "r")

    # 查找当前位置
    position = f.tell()
    print "当前文件位置 : ", position

    # 重新设置位置
    f.seek(-3,2)

    # 读取到的数据为:文件最后3个字节数据
    str = f.read()
    print "读取的数据是 : ", str

    f.close()

4 文件的重命名、删除

有些时候,需要对文件进行重命名、删除等一些操作,python的os模块中都有这么功能

<1>文件重命名

os模块中的rename()可以完成对文件的重命名操作

rename(需要修改的文件名, 新的文件名)


    import os

    os.rename("毕业论文.txt", "毕业论文-最终版.txt")

<2>删除文件

os模块中的remove()可以完成对文件的删除操作

remove(待删除的文件名)


    import os

    os.remove("毕业论文.txt")

文件夹的相关操作

实际开发中,有时需要用程序的方式对文件夹进行一定的操作,比如创建、删除等

就像对文件操作需要os模块一样,如果要操作文件夹,同样需要os模块

<1>创建文件夹


    import os

    os.mkdir("张三")

<2>获取当前目录


    import os

    os.getcwd()

<3>改变默认目录


    import os

    os.chdir("../")

<4>获取目录列表


    import os

    os.listdir("./")

<5>删除文件夹


    import os

    os.rmdir("张三")

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325149707&siteId=291194637