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对脚 ...
随机推荐
- C++ 工程师养成 每日一题fourth (reverse的使用)
题目: 将一句话的单词进行倒置,标点不倒置. 这道题最简单的解法是使用algorithm提供的reverse()函数 具体步骤我写在代码注释里面: #include <string> #i ...
- 嵌入式02 STM32 实验05 蜂鸣器
蜂鸣器:是一种一体化结构的电子讯响器.主要分为分压式蜂鸣器和电磁式蜂鸣器两种类型. 一.有源/无源蜂鸣器(不是指是否带电源,而是有没有自带震荡电路) 1.有源蜂鸣器:有源蜂鸣器自带震荡电路,一通电就会 ...
- 实现动态WEB内容
在 system1 上配置提供动态web内容,要求: 1.动态内容由名为 wsgi.group8.example.com 的虚拟主机提供 2.虚拟主机侦听在端口 8909 3.从 http://ser ...
- SVN客户端教程
.SVN是一个自由/开源的版本控制系统,一组文件存放在中心版本库,记录每一次文件和目录的修改,Subversion允许把数据恢复到早期版本,或是检查数据修改的历史,Subversion可以通过网络访问 ...
- RabbitMQ Policy的使用
RabbitMQ作为最流行的MQ中间件之一,广泛使用在各类系统中,今天我们就来讨论一下如何通过Policies给RabbitMQ中已经创建的Queue添加属性和参数. Policise 的作用 通常来 ...
- Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot
需求缘起:有人在群里@我:请教群主大神一个问题,spring boot + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spri ...
- Entity framework Core 数据库迁移
本文转自https://www.cnblogs.com/zmaiwxl/p/9454177.html 初始化数据库 1.添加初始迁移 Add-Migration init 向“迁移”目录下的项目添加以 ...
- 1.ASP.NET Core 中向 Razor Pages 应用添加模型
右键单击“RazorPagesMovie”项目 >“添加” > “新建文件夹”. 将文件夹命名为“Models”.右键单击“Models”文件夹. 选择“添加” > “类”. 将类命 ...
- c#mysql数据库备份还原
1:引用dll MySql.Data.dll, MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql { public static str ...
- MVC学习笔记(二)—用EF创建数据库
1.创建一个空项目 2.在项目中创建EFCore的类库 3.在NuGut控制台为EFCore项目中安装entity 3.1 命令为:Install-Package EntityFramework(在 ...