python读写文件

open函数

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
    Open file and return a stream.  Raise OSError upon failure.

    file is either a text or byte string giving the name (and the path
    if the file isn't in the current working directory) of the file to
    be opened or an integer file descriptor of the file to be
    wrapped. (If a file descriptor is given, it is closed when the
    returned I/O object is closed, unless closefd is set to False.)

    mode is an optional string that specifies the mode in which the file
    is opened. It defaults to 'r' which means open for reading in text
    mode.  Other common values are 'w' for writing (truncating the file if
    it already exists), 'x' for creating and writing to a new file, and
    'a' for appending (which on some Unix systems, means that all writes
    append to the end of the file regardless of the current seek position).
    In text mode, if encoding is not specified the encoding used is platform
    dependent: locale.getpreferredencoding(False) is called to get the
    current locale encoding. (For reading and writing raw bytes use binary
    mode and leave encoding unspecified.) The available modes are:

    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================

其中,最常用的是默认和写入。

读入文件并提取文件内容

file = open("ex12.txt")
txt  = file.read()
print(txt)

ex12.txt内容

two three
three four
four five

读取文件并写入新内容

用"w"模式打开文件,可以对文件进行写入,打开时会默认清楚原内容,故写入的位置默认是文件的开头。

file = open("ex12.txt","w")
file.write("111")
file.close()

此时我们再次查看文件ex12.txt的内容,发现内容变为

111

之前的所有内容被删除,取而代之的是新写入的数字111。

上述代码中,如果不关闭文件,继续进行写入,此时写入的位置是上一次的结尾处,私以为可以像光标位置一样进行理解。

file = open("ex12.txt","w")
file.write("111")
file.write("222")
file.close()

此时查看ex12.txt,发现其内容变为

111222

可见多次写入是前后紧凑连接的,如果有文本格式上的其他要求,可以搭配制表换行以及格式化字符来实现,下文给出一个例子:

file = open("ex12.txt","w")
file.write("111\t222\n333")
file.close()

可查看ex12.txt的内容变为

111	222
333

在改写后的文本中我们可以看到制表和换行的存在。

新的问题出现了。如果先写入然后关闭文件,然后再读入再写入会怎样呢,答案是会输出第二次的写入内容,因为这是两次独立的改写,第二次会覆盖第一次的内容。例如

file = open("ex12.txt","w")
file.write("111")
file.close()
filee=open("ex12.txt","w")
filee.write("222")
filee.close()

运行结束后查看ex12.txt,发现内容变为

222

这是第二次的写入内容,其效果与只使用第二次写入是一样的。

用上述方法结合之前的内容提取方法可以实现文件内容的复制。

infile = open("ex12.txt")
outfile= open("ex21.txt","w")
data = infile.read()
outfile.write(data)
outfile.close()

运行结束之后,ex12.txt与ex2.txt的内容都是

222

**
在指定位置进行写入也是可以实现的,但是我还不会-_-。

你可能感兴趣的:(python学习记录,python)