博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 003 os模块 example
阅读量:5132 次
发布时间:2019-06-13

本文共 2268 字,大约阅读时间需要 7 分钟。

import osfor tmpdir in ('/tmp',r'C:/Users/Administrator/PycharmProjects/'):    if os.path.isdir(tmpdir):        breakelse:    print 'No temp directory available'    tmpdir = ''if tmpdir:    os.chdir(tmpdir)    cwd = os.getcwd()    print '*** current temporary directory'    print cwd    print '*** creating example directory'    while os.path.exists('example'):        break    else:        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...'    fobj = open('test','w')    fobj.write('foo\n')    fobj.write('bar\n')    fobj.close()    print '*** updated directory listing:'    print os.listdir(cwd)    print '***renaming "test" fo "filetest.txt"'    os.rename('test','filetest.txt')    print '***full file pathname'    path = os.path.join(cwd,os.listdir (cwd)[0])    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:'    fobj = open(path)    for eachLine in fobj:        print eachLine    fobj.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'

输出

C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/untitled/ospathex.py*** current temporary directoryC:\Users\Administrator\PycharmProjects*** creating example directory*** new working directoryC:\Users\Administrator\PycharmProjects\example*** original directory listing:[]*** creating test file...*** updated directory listing:['test']***renaming "test" fo "filetest.txt"***full file pathnameC:\Users\Administrator\PycharmProjects\example\filetest.txt***(pathname,basename) ==('C:\\Users\\Administrator\\PycharmProjects\\example', 'filetest.txt')***(filename,extension) ==('filetest', '.txt')***displaying file contents:foobar***deleting test file***updated directory listing:[]*** deleting test directory*** DONE

posted on
2017-06-11 16:06 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/EWWE/p/6985438.html

你可能感兴趣的文章
hyphenation
查看>>
三类人最好别吃蜂蜜
查看>>
常用animation动画
查看>>
避免unicode字符被截断的方法
查看>>
获取一些系统和用户的通用属性
查看>>
什么是响应式
查看>>
JAVA过滤emoji表情包
查看>>
flex布局学习
查看>>
EasyUI中Datagrid列定位方法
查看>>
二叉树的概念和基本术语
查看>>
Django处理ajax请求
查看>>
LeetCode:202. 快乐数
查看>>
Javascript 跨域
查看>>
10.15 sigstjmp以及siglongjmp函数
查看>>
JDBC如何判断数据库的表是否存在
查看>>
挂个AC自动机
查看>>
[bzoj5158][Tjoi2014]Alice and Bob
查看>>
远程电脑备份与还原数据库
查看>>
java序列化
查看>>
JS实现div居中
查看>>