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 ...
随机推荐
- angularjs 选项卡 --- 自定义属性
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- SVG 2D入门6 - 坐标与变换
坐标系统 SVG存在两套坐标系统:视窗坐标系与用户坐标系.默认情况下,用户坐标系与视窗坐标系的点是一一对应的,都为原点在视窗的左上角,x轴水平向右,y轴竖直向下:如下图所示: SVG的视窗位置一般是由 ...
- hdu 2054
Ps:WA了无数次,,简直成了心病..今天终于AC了..先取整数部分,去零,判断位数相等否,再比较.如果相等,再取小数部分,去零,比较,输出....好烦... 代码; #include "s ...
- 5分钟学习maven
英文原地址:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html 一.前提 需要懂得如何在计算机上安装软件 ...
- 设置vs2008代码区的背景色
编写程序.调试代码时,是不是感觉文字的白色背景有些刺眼呢?vs2008 给我们提供了改变背景色的功能. 依次展开:工具->选项->环境->字体和颜色,在“显示项--纯文本--项背景色 ...
- 使用Qemu调试内核
利用Qemu进行内核源码级调试 http://blog.csdn.net/gdt_a20/article/details/7231652 用Qemu调试Linux内核 http://blog.chin ...
- Java容器类接口的选择
我们知道Java容器类实际提供了四类接口:Map,List,Set和Queue,如下图所示,每种接口都有不止一个版本的实现,如果在实际编写程序时需要使用某种接口时该如何选择. 从Oracle的Java ...
- [Camel Basics]
Define routes: Either using Spring xml or Java DSL. Spring xml: <camelContext> <routeBuilde ...
- 1、C#基础整理(进制转换的方法)
进制 二进制转换为十进制(展权相加法) 110101 =1*2^5+1*2^4+0*2^3+1*2^2+0*2^1+1*2^0 1. 写2 2. 标指数(从右到左) 3. 乘系数(一一对应) 4 ...
- SWUST0249 (凸包面积)
type node=record x,y:longint; end; ; var k,q,qq:longint; sum:double; f,g:..maxn] of node; m,i,j,a,b: ...