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 ...
随机推荐
- VM虚拟机中Ubuntu无法连接网络
之前Ubuntu是可以上网的,但是今天打开后发现上不了网了,经过一番折腾,弄好了,记录下. 方案一:将网络连接设置为自定义NAT VM ->设置-> 硬件->网络适配器 这么已修改就 ...
- mysql 命令行操作
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- hdu 2047
PS:又是上课偷懒..去递推.. 代码: #include "stdio.h"#include "math.h"long long dp[55];long lo ...
- 10年山东省赛-E-最短路
题目连接:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2155&cid=1430 题意:输入一个n个节点,m条边的图,然后k条记录,纪录 ...
- Comments
Nothing can be quite so helpful as a well-placed comment.Nothing can clutter up a module more than f ...
- Unity3D ShaderLab 各向异性高光
Unity3D ShaderLab 各向异性高光 各向异性时一种模拟物体表面沟槽方向性的高光反射类型,它会修改或延伸垂直方向上的高光.当我们想模拟金属拉丝高光的时候,它非常适合.下面就一步一步实现. ...
- PHP 防范xss攻击
XSS 全称为 Cross Site Scripting,用户在表单中有意或无意输入一些恶意字符,从而破坏页面的表现! 看看常见的恶意字符XSS 输入: 1.XSS 输入通常包含 JavaScript ...
- 用Inno Setup来解决.NetFramework安装问题
用Inno Setup来解决.NetFramework安装问题 2010-03-01 09:59:26 标签:.Net Framework Setup 休闲 Inno 原创作品,允许转载,转载时请务必 ...
- iOS-TCP/IP、Http、Socket的区别
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- linux的ftp使用方法
linux下常用FTP命令 1. 连接ftp服务器 以下是在服务器为Ubuntu环境下操作: (1)首先需要安装vsftp软件,已经安装的可跳过. 1.更新软件源 #apt-get update 2 ...