Python应用之-file 方法

#!/usr/bin/env python
# *_* coding=utf-8 *_*

"""
desc: 文件方法

#############################
file.read()         #read([size]) -> read at most size bytes, returned as a string.
file.readline()     readline([size]) -> next line from the file, as a string.
file.readlines()
       readlines([size]) -> list of strings, each a line from the file.
        Call readline() repeatedly and return a list of the lines so read.
        The optional size argument, if given, is an approximate bound on the
        total number of bytes in the lines returned.
file.xreadlines()     xreadlines() -> returns self.

file.write()      write(str) -> None.  Write string str to file.
file.writelines()
           writelines(sequence_of_strings) -> None.  Write the strings to the file.
            Note that newlines are not added.  The sequence can be any iterable object
            producing strings. This is equivalent to calling write() for each string.

file.truncate()
      truncate([size]) -> None.  Truncate the file to at most size bytes.
      Size defaults to the current file position, as returned by tell().

file.seek()
       seek(offset [,whence])方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
                 如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。

file.flush()
file.tell()         current file position, an integer (may be a long integer).
file.next()              

file.close()            

############不常用的方法#############
file.closed
file.mode
file.name               

file.isatty()              # true or false.  True if the file is connected to a tty device.
file.readinto()             #不用用这个
file.encoding                # 不常用
file.errors                  # 不常用
file.newline                 # 不常用
file.softspace
file.fileno()
                fileno() -> integer "file descriptor".
                This is needed for lower-level file interfaces, such os.read().   

###############################
version: 1.0
"""

fo= open("e:/temp.txt",‘r‘)
print "文件名: ", fo.name
print "是否已关闭 : ", fo.closed
print "访问模式 : ", fo.mode
print "末尾是否强制加空格 : ", fo.softspace

  

原文地址:https://www.cnblogs.com/68xi/p/8547399.html

时间: 2024-07-30 02:48:15

Python应用之-file 方法的相关文章

Python的内置方法,abs,all,any,basestring,bin,bool,bytearray,callable,chr,cmp,complex,divmod

Python的内置方法 abs(X):返回一个数的绝对值,X可以是一个整数,长整型,或者浮点数,如果X是一个复数,此方法返回此复数的绝对值(此复数与它的共轭复数的乘积的平方根) >>> abs(3+2j) 3.605551275463989 >>> abs(3-2j) 3.605551275463989 all(iterable):如果迭代器的所有元素都是true,或者空迭代器,则此方法返回true. any(iterable):迭代器只要有一个元素为false,或者空

浅谈python中的一般方法、静态方法(staticmethod)和类方法(classmethod)

我们先来简单谈谈python类中一般方法.静态方法和类方法的区别. 1.类中的一般方法 一般方法在定义的时候,需要有表示类实例的参数(通常以self表示,例如def foo(self,arg1,arg2--)),一般方法不能通过类名.方法名()来调用,必须先创建类的实例,然后通过实例.方法名()来调用. 2.类中的静态方法 类中的静态方法用"@staticmethod"来修饰,静态方法在定义的时候,不需要表示类的实例,可想类外的方法定义一样.静态方法可以通过类名.方法名()和实例.方法

"#!/usr/bin/python: No such file or director"引发的编码问题

问题描述 我自己写了一个Python脚本,在Linux服务器和我的Mac上运行都报错: ./build_system.py: line 1: #!/usr/bin/python: No such file or directory 在网上搜索了半天,发现都说都问题原因是第一行尾部写入了Windows都回车"\r\n",导致解析成了"python\r"而不是"python",因而报错是": No such file or director

编程中遇到的Python错误和解决方法汇总整理

这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下 开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析,并持续更新,方便以后查询,学习.知识在于积累嘛!微笑+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++错误: 复制代码代码如下: >>> def f(x, y):      print x, y  >>> t

转 Python执行系统命令的方法

传送门 Python执行系统命令的方法 http://www.linux-field.com/?p=15 Python中执行系统命令常见方法有两种: 两者均需 import os (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command (a string) in a subshell. # 如果再命令行下执行,结果直接打印出来 1 >>> os.

关于python -os.mkdir(str)方法的使用记录

这几天在学习python,从昨天开始安装了ubuntu系统以后,就开始研究这个备份文件的例子,可是无论如何,总是不成功,不是说 OSError: [Errno 2] No such file or directory: 就是说 OSError: [Errno 13] Permission denied: 这些错误都是因为一个os.mkdir()的系统模块的方法,终于是把我惹急了,在这个方法之前添加了测试输出语句,完全可以执行,很明显就是这个方法搞得不对,google了一下os.mkdir();找

Python OS 文件/目录方法

Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作目录 3 os.chflags(path, flags) 设置路径的标记为数字标记. 4 os.chmod(path, mode) 更改权限 5 os.chown(path, uid, gid) 更改文件所有者 6 os.chroot(path) 改变当前进程的根

python 字符串内置方法整理

编码相关内置方法: (1)    str.encode(encoding='utf-8'):返回字符串编码,encoding指定编码方式. >>> a = 'I love Python' >>> a.encode(encoding='utf-8') b'I love Python' >>> a.encode(encoding='gbk') b'I love Python' >>> b.encode(encoding='utf-8')

Python中的sort()方法的使用

本文和大家分享的主要是python中的sort()方法相关知识,一起来看看吧,希望对大家学习python有所帮助. 一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)iterable指定要排序的list或者iterable,不用多说: (2)cmp为函数,指定排序时进行比较的函数,可以指定一个函数或者lambda函数,如: students为类对象的list