Python学习笔记——文件
import os
for tempdir in ('/tmp',r'c:\temp'):
if os.path.isdir(tempdir):
break
else:
print ('no temp directory available')
tempdir=''
if tempdir:
os.chdir(tempdir)
cwd=os.getcwd()
print ('****current temporary directory:')
print (cwd) print('****creating example directory:')
os.mkdir('example')
os.chdir('example')
cwd=os.getcwd()
print ('****new working directory:')
print (cwd)
print("****original directory listing:")
print(os.listdir(cwd))
print ('****creating test file...')
f=open('test','w')
f.write('foo\n')
f.write('bar\n')
f.close()
print('****updated directory listing:')
print(os.listdir(cwd)) print("****renaming 'test'to'filetest.txt'")
os.rename('test','filetest. txt')
print ('****updated directory listing:')
print (os.listdir(cwd)) path=os.path.join(cwd,os.listdir(cwd)[0])
print ('****full file pathname:')
print (path)
print('****(pathname,basename) == ')
print (os.path.split(path))
print ('****(filename,extension)==')
print (os.path.splitext(os.path.basename(path))) print ('****displaying file contents:')
f=open(path)
for eachLine in f:
print(eachLine)
f.close() print ("****deleting test file")
os.remove(path)
print ('****updated directory listing:')
print (os.listdir(cwd))
os.chdir(os.pardir)
print ('****deleting test directory')
os.rmdir('example')
print('****Done')
Python学习笔记——文件的更多相关文章
- [Python学习笔记]文件的读取写入
文件与文件路径 路径合成 os.path.join() 在Windows上,路径中以倒斜杠作为文件夹之间的分隔符,Linux或OS X中则是正斜杠.如果想要程序正确运行于所有操作系统上,就必须要处理这 ...
- python学习笔记:文件操作和集合(转)
转自:http://www.nnzhp.cn/article/16/ 这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句 ...
- Python学习笔记——文件写入和读取
1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...
- Python学习笔记——文件操作
python中,一切皆对象. 一.文件操作流程 (1)打开文件,得到一个文件句柄(对象),赋给一个对象: (2)通过文件句柄对文件进行操作: (3)关闭文件. 文件对象f通过open()函数来创建 ...
- python学习笔记---文件的操作
数据的保存: 1.内存:常用的变量2.文件:文本内容,二进制的文件内容3.数据库: 读文件:1.要读取的文件路径一定要存在.2.打开存在的文件:open函数 参数1:文件的路径,相对的或者是绝对 ...
- python学习笔记--文件重命名,删除及文件夹
文件重命名 import os os.rename('123.txt','456.txt') 删除文件 >>> import os >>> os.remove('4 ...
- 03 python学习笔记-文件操作(三)
本文内容主要包括以下方面: 1. 文件操作基本认识2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 文件的修改 一.文 ...
- python 学习笔记---文件处理
1.打开文件读取数据 f =open(“wenjian.txt”,"r") print(f) f.close() 直接变成列表--->list(f) for each_lin ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
随机推荐
- PHP面向对象的继承
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转)JS中innerHTML,innerText,value
原文:http://holysonll.blog.163.com/blog/static/21413909320134111054352/ JS中innerHTML,innerText,value 2 ...
- C++11 实现 argsort
看python发现有这么个api,感觉很实用,想着stl里会不会有这个呢?查了半天毫无结果.于是用lambda自己实现了下. 以vector为例 template<typename T> ...
- oracle的存储结构
表空间 当一个用户被创建以后,随之就要为用户分配数据存储的空间,这在oracle中成为“表空间”(Tablespace). 在数据库中创建用户时,基于应用性能和管理的考虑,最好为不同的用户创建独立的表 ...
- MATLAB简单实现ID3
再看<MATLAB数据分析与挖掘实战>,简单总结下今天看到的经典的决策树算法——ID3. ID3:在决策树的各级节点上,使用信息增益的方法作为属性的选择标准,来帮助确定生成每个节点时所应采 ...
- socket编程(Linux)
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...
- iOS开发:cocoapods的使用
Cocoapods是OS X和iOS下的一个第三方类库管理工具,通过CocoaPods工具我们可以为项目添加各种依赖库,减少了我们手动引入库需要的各种配置,同时使用cocoapods可以方便的查找新的 ...
- C语言基础:进制转换,变量,常量,表达式,基本数据类型,输出函数,输入函数,运算符. 分类: iOS学习 c语言基础 2015-06-10 21:39 25人阅读 评论(0) 收藏
二进制:以0b开头,只有0和1两种数字.如0101 十进制:0~9十个数字表示.如25 十六进制:以0~9,A~F表示,以0X开头.如0X2B 十进制转换为X进制:连除倒取余 X进制转换为十进制:按权 ...
- CODEVS1380 没有上司的舞会 (树形DP)
f[i,0] 表示 第i个人不参加舞会 f[i,1] 表示 第i个人参加舞会 f[i,1]=sigma(f[j,0])+v[i] j 为 i 的孩子 f[i,1]=sigma(max(f[j,0] ...
- 用正则表达式获取所有img标签
public static string ReplaceOrAddImageTitle(string content, string title) { Regex reg = new Regex(@& ...