未加星标

Python os module- File operations in Python using python os module

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

python provides os module which can be used to perform File operations like - create, read, write and delete. Below program performs following operation -

1. Check for a directory and create if it does not exist

2. Create a file .

3. Write into file

4. Rename file

5. Delete files

6. Delete Directory

Sample program to illustrate various file operation using os module of python :- import os import sys import tempfile def dofileoperation(): dirExist=0 tempdir=tempfile.gettempdir() if os.path.isdir(tempdir): print "temp directory exist!!" dirExist=1 else: print "No temp directory exists, \ so create user defined directory" dir="C:\tempPython" create_temp_dir(dir) dirExist=1 if dirExist==1: os.chdir(tempdir) current_working_dir = os.getcwd() print "Current working directory is " + current_working_dir print "Create 'example' directory if does not exist" if os.path.isdir('example'): pass else: create_dir('example') #change directory to example os.chdir('example') current_working_dir=os.getcwd() print "\nNew working directory is " + current_working_dir print "Creating three file in current working directory " for i in range(1,3): file_handle=create_file_writemode('input'+str(i)) write_into_file(file_handle,'Hello file\nI \ am first line\nI am second line') print "\nCurent directory listing are " current_dir_listing(current_working_dir) print "\nDisplay fully qualified of all \ file in current directory" full_qualified_name(current_working_dir) first_file= os.path.join(current_working_dir,\ os.listdir(current_working_dir)[0]) print "\nDisplaying file contents of file: " + first_file read_file_content(first_file) print "Rename first file in current directory " file_rename(first_file,first_file+'_new') print "\nCurent directory listing after file rename " current_dir_listing(current_working_dir) print "\nDeleting all files in current directory" delete_files_indir(current_working_dir) print "\nCurent directory listing are " current_dir_listing(current_working_dir) print "\nDeleting working directory 'example' " delete_dir('example') print "\nChecking existance of example dir" if os.path.isdir('example'): print 'example directory exists!!' else: print 'example directory deleted successfully !!' def create_dir(dir): os.mkdir(dir) def delete_dir(dir): #print os.pardir os.chdir(os.pardir) #print os.getcwd() os.rmdir(dir) def create_file_writemode(filename): return open(filename,'w') def get_filehandle(filename): return open(filename) def write_into_file(file_handle,text): file_handle.write(text) file_handle.close() def full_qualified_name(current_working_dir): for filename in os.listdir(current_working_dir): print os.path.join(current_working_dir,filename) def current_dir_listing(current_working_dir): print os.listdir(current_working_dir) def read_file_content(filename): file_handle = open(filename) allLines = file_handle.readlines() for eachline in allLines:

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Python
分页:12
转载请注明
本文标题:Python os module- File operations in Python using python os module
本站链接:http://www.codesec.net/view/480917.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(28)