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 ...
随机推荐
- C#获取项目程序及运行路径的方法
1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”: 2.c# winform用 A:“Applicat ...
- 使用Dmitry Sklyarov的方法来破解一款流行的4G调制解调器
逆向工程师如果对设备代码和固件系统进行检测时,发现了许多经过加密处理的固件文件,该怎么办?在本文中我将通过一个真实的故事来教大家如何利用一些基础的计算机知识以及简单的逻辑来应对这一问题. 因为这篇文章 ...
- 101+ Manual and Automation Software Testing Interview Questions and Answers
101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...
- wddm 部署问题解决
在把wddm部署到一台老的服务上的 windows server 2003 上时遇到了问题,之前也在 windows server 2003 上装过,但是并没有遇到问题,估计和服务器比较老有关系. 问 ...
- PAT (Basic Level) Practise:1040. 有几个PAT
[题目链接] 字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问一共可 ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- 关于Highcharts图表组件动态修改属性的方法(API)总结之Series
Highcharts图表组件内的Series很重要,如果说Categries是其大脑,那么Series就是其心脏.这两者才是Highcharts图表组件的重中之重啊.接下来就为大家介绍关于设置Seri ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- tyvj 1402 dp
P1402 [NOIP2010]乌龟棋 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2010提高组复赛第二题 描述 小明过生日的时候,爸爸送给他一 ...
- Educational Codeforces Round 15 C 二分
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...