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
Python应用之-file 方法的更多相关文章
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- Python——特殊属性与方法
Python 对象 中以双下划线开头和结尾的属性称为特殊属性,由于对象的方法也属于属性,因此以双下划线开头和结尾的方法称为特殊方法.对这些对象执行一些特定的运算时,Python会自动视图调用这些实例的 ...
- python动态绑定属性和方法
基于Python 2.7.13测试. Python是动态语言,在类定义了之后,还可以动态地绑定属性和方法. 下面先来看怎么给类的实例动态地绑定属性和方法. >>> class Stu ...
- Python OS 文件/目录方法
Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...
- python 输入输出,file, os模块
Python 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout ...
- Python 文件对象和方法
Python文件对象和方法 1.打开和关闭文件 Python提供了必要的函数和方法进行默认情况下的文件基本操作,我们可以用file对象做大部分文件操作. open()方法 我们必须先用Python内置 ...
- 编程中遇到的Python错误和解决方法汇总整理
这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下 开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析, ...
- 转 Python执行系统命令的方法
传送门 Python执行系统命令的方法 http://www.linux-field.com/?p=15 Python中执行系统命令常见方法有两种: 两者均需 import os (1) os.sys ...
- 调用python脚本报错/usr/bin/env: python : No such file or directory
一.调用python脚本报错 /usr/bin/env: python: No such file or directory 二.解决方法 原因是在windows上编写的脚本,使用dos2unix对脚 ...
随机推荐
- [转帖]NSO到底是个什么样的公司?揭秘三叉戟0day的缔造者
NSO到底是个什么样的公司?揭秘三叉戟0day的缔造者 dawner2016-09-07共248912人围观 ,发现 3 个不明物体其他安全报告 https://www.freebuf.com/art ...
- Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块
路由跳转 三种方式: $router.push / $router.go / router-link to this.$router.push('/course'); this.$router.pus ...
- 【leetcode】501. Find Mode in Binary Search Tree
class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...
- 记CentOS 发布.NET Core 2.0
centos 7.x sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e & ...
- Golang中的RegExp正则表达式用法指南
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- Python 将中文、字母转成数字
Outline 把中文汉字或者英文字母或者特殊字符转换成数字. (实质是字符转成对应ASCII码) 转换 将中文汉字转成数字: ord('单个中文汉字') 反转: chr(21704) 将英文字母转成 ...
- java中四种权限修饰符区别
总的概括:public > protected > (default) > private 细分见下表格: 权限修饰符 public protected (default) priv ...
- Java实现树的遍历以及打印(递归,非递归)
import java.util.LinkedList; import java.util.Stack; public class BinarySearchTree1<E extends Com ...
- LocalDateTime&LocalDate&LocalTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime ti ...
- 2.live555源码分析----服务端doEventLoop()函数分析
上一篇博客说道,live555服务端main函数做的最后一件事就是调用如下代码陷入死循环: env->taskScheduler().doEventLoop(); // does not ret ...