python2学习------基础语法5(文件操作)
1、文件内容读取
a.txt
teestttttttttttttt
teestttttttttttttt
teestttttttttttttt
teestttttttttttttt
teestttttttttttttt
readFile.py
# 格式
file = open(文件路径,操作模式);
file.read(); # file.readline(); #按行读取
file.close();
# example
def readText(filePath,mode):
file = open(filePath,mode); # 打开文件并制定文件操作模式
#file.seek(6); # 跳过多少字符进行读取
while True:
line = file.readline(); # 分行全部读取
if line == '' or line is None: # 去除空行
break;
print line;
'''
print file.read(); # 全部读取
'''
file.close();
#调用文件读取函数
readText(R"E:\a.txt","r");# 文件路径具体制定
2、文件内容写入
# 用法
file=open(文件路径,操作权限);
file.write(内容);
file.close(); # example
def writeText(filePath,mode):
file=open(filePath,mode);
i=0;
while i<5:
file.writelines("teestttttttttttttt\n");
i=i+1;
file.close(); # 调用函数执行文件创建
writeText(r"E:\a.txt","w");
3、显示指定路径下的文件列表
# os模块
os.listdir("指定路径"); # example
#!/usr/bin/python2.7
# __*__ encoding:UTF-8 __*__
import os;
fileDir=os.listdir("./");
for i in fileDir:
print i;
4、便利指定路径下的文件目录
#!/usr/bin/python2.7
#__*__ coding: UTF-8 __*__
import os; class FileTest:
def __init__(self):
pass;
def __getFileAndDir__(self,filePath):
tmp=os.walk(filePath,topdown=True);
for root,dirs,files in tmp:
# 遍历输出目录
for i in dirs:
print os.path.join(root,i);
# 遍历输出文件
for i in files:
print os.path.join(root,i);
a=FileTest();
a.__getFileAndDir__("F:\\test");
运行结果:

5、待定
python2学习------基础语法5(文件操作)的更多相关文章
- Python学习—基础篇之文件操作
文件操作 文件操作也是编程中需要熟练掌握的技能,尤其是在后台接口编写和数据分析过程中,对各种类型的文件进行操作,获取文件信息或者对信息进行存储是十分重要的.本篇博客中将主要对常见的文本格式文件和Exc ...
- python2学习------基础语法5(常用容器以及相关操作)
1.list(列表) #生成数据list a=[x for x in range(10)]; #print a; #遍历list for i in a: pass; #print i; #追加元素 a ...
- python2学习------基础语法1 (变量、分支语句、循环语句、字符串操作)
1.变量类型 Numbers(数字):int,float,long String(字符串) List(列表) tuple(元组) dict(字典) bool(布尔):True,False # 删除变量 ...
- python2学习------基础语法4(模块)
1.整体结构层次(a.py,b.py) 目标:b.py文件中导入a.py里面定义的class A,并调用A类里面的属性或相关方法. 2.模块导入 <1> __init__.py < ...
- Python基础语法之文件操作
1 读文件 1.1 简单读文件 f = open('text', 'r') # f是文件句柄 data = f.read() # read方法可以加整型参数,是几就读几个字符 print(data) ...
- python2学习------基础语法3(类、类的继承、类成员函数、防御式编程)
1.类的定义以及实例化 # 类定义 class p: """ this is a basic class """ basicInfo={&q ...
- python2学习------基础语法2(函数)
1.函数 # 无参数函数 def loopTest2(): a=1; while a<40: print a; a=a+1; if a==35: continue; else: print 'o ...
- Windows phone 8 学习笔记(2) 数据文件操作
原文:Windows phone 8 学习笔记(2) 数据文件操作 Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方 ...
- python学习笔记-(七)python基础--集合、文件操作&函数
本节内容 1.集合操作 2.文件操作 3.字符编码与转码 4.函数操作 1.集合操作 集合是一个无序的.不重复的数据组合: 1.1 常用操作 它的作用是: 1)自动去重:列表变成集合,自动去重: &g ...
随机推荐
- Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用log的复杂度加快了更新速度,也用了区间查询(查询当前区间向右直至最右中以当前区间端点向右一段区间的和中最大的那一段的和),也用lo ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- layui弹窗全屏显示
var index =layer.open({ id: 'id', type: 2, area: ['100%', '100%'], fix: false, maxmin: true, shadeCl ...
- jupyter配置 nbextension
jupyter contrib nbextension install --user --skip-running-check No module named 'pysqlite2' 解决方法:打开此 ...
- GIT使用教程——命令详解
$ git init 当前目录建立GIT可以管理的仓库(版本库),生成一个.git的隐藏文件夹 $ git add <filename> 将工作区的文件修改添加到版本库的暂存区 $ git ...
- JavaScript学习笔记----- 继承的实现及其原理
按照自己在极客上学习的顺序整理了一下,参考了几位前辈的随笔,十分感谢: 参见http://blog.yemou.net/article/query/info ...
- C语言入门---第七章 C语言函数
函数就是一段封装好的,可以重复使用的代码,它使得我们的程序更加模块化,不需要编写大量重复的代码.函数可以提前保存起来,并给它起一个独一无二的名字,只要知道它的名字就能使用这段代码.函数还可以接收数据, ...
- 02-06Android学习进度报告六
今天学习了关于Android开发中常用的两个知识,即对话框和悬浮框. 首先我学习了对话框的基本使用流程 Step 1:创建AlertDialog.Builder对象: Step 2:调用setIcon ...
- webpack的配置文件[webpack.config.js]
如果项目里没有webpack.config.js这个文件,webpack会使用它本身内置在源码里的配置项. webpack.config.js这个配置名称可以通过指令修改 npx webpack -- ...
- PL-USB2-BLASTER 使用说明
PL-USB2-BLASTER 使用说明 PL-USB2-BLASTER就是USB BLATER II烧录器.官方文档在https://www.intel.com/content/dam/www/pr ...