【Python之旅】第二篇(一):Pyt

发布时间:2019-09-12 07:58:28编辑:auto阅读(1947)

    说明:

        主要是file()和open()函数的使用,但在查open()函数的帮助时,会有下面的说明:

    >>> help(open)
    ……
    Open a file using the file() type, returns a file object.

        因此,两个函数其实都是一样的,下面只用file()。

        在列举file()的作用时,使用help即是很好的方法,下面则是应重点关注的内容:

    close(...)
     |      close() -> None or (perhaps) an integer.  Close the file.
    flush(...)
     |      flush() -> None.  Flush the internal I/O buffer.
    readline(...)
     |      readline([size]) -> next line from the file, as a string. 
    readlines(...)
     |      readlines([size]) -> list of strings, each a line from the file.
    seek(...)
     |      seek(offset[, whence]) -> None.  Move to new file position.
    tell(...)
     |      tell() -> current file position, an integer (may be a long integer).
    write(...)
     |      write(str) -> None.  Write string str to file.
    writelines(...)
     |      writelines(sequence_of_strings) -> None.  Write the strings to the file.
    xreadlines(...)
     |      xreadlines() -> returns self.




    1.创建文件


    --基本格式:

    f = file('test.txt', 'w')
    f = write('Hello World!')
    f.close()

    ·w:写模式,文件不存在就创建,存在则自动覆盖原来的内容,只能写,不能读;

    ·w+:写读模式,但一开始还是会清空原来文件内容,只是在写文件之后可以读取;

    ·写的内容放在内存当中,如果要写入磁盘,可以f.close()关闭文件或f.flush()实时写入磁盘;

    ·不可以实时改变模式,只能把文件关闭后,再次打开时定义模式;


    --实例:

    >>> f = file('test.txt', 'w')
    >>> f.write('Hello World!')
    >>> f.flush()
    xpleaf@xpleaf-machine:~/seminar6/day2$ more test.txt
    Hello World!


    --write()与writelines()

    ·前者写入的内容只能是字符串,后者则可以写入列表:

    >>> f.write(['a', 'b', 'c'])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected a character buffer object
    >>> f.writelines(['a', 'b', 'c'])
    >>>


    --f.close()的重要说明

    ·如果没有f.close(),则在程序运行结束后,系统会自动帮我们关闭文件;

    ·长时间运行的程序,需要打开并编辑文件(如用'a'模式),没有关闭文件,会导致文件内容无法保持一致性的问题(如果系统中有其他程序需要编辑该文件);

    ·Linux中的Vim编辑器自带文件锁定功能,即不能同时编辑同一文件;

    ·Python中文件的锁是没有加上的,需要开发者自行为文件加锁。




    2.读取文件与遍历文件内容


    --基本格式:

    f = file('test.txt', 'r') ===>可以不加'r',默认就是该模式
    f = read()
    f.close()

    ·r:默认;

    ·r+:读写模式,可以尝试使用,每读取一行,指针就跳到下一行,写的时候,就直接覆盖掉指针指的这一行;

    ·rb:在windows平台下编辑的文件,在linux中用python进行读取时,模式要选择“rb”,否则有可能会出现乱码的现象,即跨平台的文件都要注意此点;


    --read()、readline、readlines()与xreadlines()

    ·前三者都是直接把文件内容全部写入内存当中,然后再全部读取或一行一行地读取;

    ·都采用迭代的方式读取,即指针最开始指向第一行,读取第一行后,指针指向下一行;


    -read()

    ·把文件内容全部读取:

    >>> f = file('test.txt', 'r')
    >>> f.read()
    "Hello World!\nI'm xpleaf.\nNice to meet you!\n"
    >>> f.read()
    ''            ===>内容已经读完,即指针已经在最后一行,后面没有内容

    ·可以用tell()查看当前指针的位置:

    >>> f.tell()
    43            ===>43,即是最后一个字符

    ·重新读取文件内容,可以f.close()后再次打开,也可以使用f.seek(0):

    >>> f.seek(0)    ===>重新寻址,让指针指向文件最开始
    >>> f.tell()
    0
    >>> print f.read()
    Hello World!
    I'm xpleaf.
    Nice to meet you!


    -readline()

    ·以字符串方式,一行一行地读取文件内容:

    >>> f.seek(0)
    >>> f.readline()
    'Hello World!\n'
    >>> f.readline()
    "I'm xpleaf.\n"
    >>> f.readline()
    'Nice to meet you!\n'
    >>> f.readline()
    ''


    -readlines()

    ·以列表的方式,一行一行地读取文件内容,一行即为列表中的一个元素:

    >>> f.seek(0)
    >>> f.readlines()
    ['Hello World!\n', "I'm xpleaf.\n", 'Nice to meet you!\n']
    >>> f.readlines()
    []

    ·因此,习惯性的用法是:修改文件内容

    >>> f.seek(0)
    >>> filelist = f.readlines()
    >>> print filelist
    ['Hello World!\n', "I'm xpleaf.\n", 'Nice to meet you!\n']
    >>> filelist[2] = 'See you next time!'
    >>> print filelist
    ['Hello World!\n', "I'm xpleaf.\n", 'See you next time!']

    ·再以w的方式打开文件,用f.writelines(filelist)的方式写入,即可实现修改文件内容的目的;


    -xreadlines()

    ·不是先把文件内容全部写入内存,而是每读取一行才写入一行,写下一行时即对前面内存中的内容进行回收;

    ·在读取较大文件时,适宜采用这种办法。


    --文件内容的遍历:使用readlines()

    >>> f = file('test.txt', 'r')
    >>> filelist = f.readlines()
    >>> for eachline in filelist:
    ...   print eachline,
    ... 
    Hello World!
    I'm xpleaf.
    Nice to meet you!




    3.文件内容追加


    --基本格式:

    f = file('test.txt', 'a')
    f = write('Hello World!')
    f.close()

    ·文件内容追加到最后一行上,如果最后一行有'\n',则追加到下一行;

    ·write只能添加字符串,如果是数值或其它类型的数据类型,则需要使用str()进行转换;


    --实例:

    >>> f = file('test.txt', 'a')
    >>> f.write('See you next time!')
    >>> f.write('I will miss you much!\n')
    >>> f.flush()
    xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
    Hello World!
    I'm xpleaf.
    Nice to meet you!
    See you next time!I will miss you much!




    4.文件内容替换


    --基本格式:

    import fileinput
    for line in fileinput.input('filepath', inplace = 1):
      line = line.replace('oldtext', 'newtext')
      print line,

    ·inplace = 1,表示要修改文件内的内容,默认值为0,表示不修改文件内容,加“print line,”时只打印内存中修改的内容(看下面例子);

    ·inplace = 1时,如果不加“print line,”,原来文件内容会为空;

    ·可以额外加backup参数,表示在修改文件内容时进行备份;


    --实例:


    -正确操作:

    >>> import fileinput
    >>> for line in fileinput.input('test.txt', inplace = 1, backup = '.ori'):
    ...   line = line.replace('Hello World!', 'Hello, everyone!')
    ...   print line,
    ... 
    xpleaf@xpleaf-machine:~/seminar6/day2$ ls -l test*
    -rw-rw-r-- 1 xpleaf xpleaf 87  9月  4 15:32 test.txt
    -rw-rw-r-- 1 xpleaf xpleaf 83  9月  4 15:19 test.txt.ori
    xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
    Hello, everyone!
    I'm xpleaf.
    Nice to meet you!
    See you next time!I will miss you much!


    -如果没有加inplace = 1时:

    >>> for line in fileinput.input('test.txt'):
    ...   line = line.replace('Nice', 'Well')
    ...   print line,
    ... 
    Hello, everyone!
    I'm xpleaf.
    Well to meet you!
    See you next time!I will miss you much!
    xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
    Hello, everyone!
    I'm xpleaf.
    Nice to meet you!
    See you next time!I will miss you much!


    -如果没有加“print line,”时:

    >>> for line in fileinput.input('test.txt'):
    ...   line = line.replace('Nice', 'Well')
    ... 
    >>> for line in fileinput.input('test.txt', inplace = 1):
    ...   line = line.replace('Hello', 'Hey')
    ... 
    xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
    xpleaf@xpleaf-machine:~/seminar6/day2$     ===>文件内容已被清空


关键字