This article example describes the python development of file operation usage. Shared for your reference, as follows:
Let's start with the official API:os-Miscellaneous operating system interfaces
Here is the demo I made:
import re import os import time # Image file path image_path = 'E:\\test\\20130627_140132Hongten.jpg' # folder path dir_path = 'E:\\test\\hongten' # File Path file_abs_path = 'E:\\test\\' # Get the current workspace directory def getcwd(): return () # Get all files and folders under the specified folder # If the specified folder does not exist, return the corresponding message def listdir(dir_path): if (dir_path): return (dir_path) else: return 'Catalog'+ dir_path + 'Does not exist' def isfile(file_path): if (file_path): return (file_path) else: return 'Documentation'+ dir_path + 'Does not exist' if __name__ == '__main__': print('The current workspace is:{0}'.format(getcwd())) print('Files and directories under the current workspace:',listdir(getcwd())) print('#' * 40) print(listdir('c:\\test')) print('#' * 40) print(isfile(image_path)) print('#' * 40) array = (image_path) print(array) #File Full Name: 20130627_140132Hongten.jpg file_full_name = array[1] name = (file_full_name) # File name: 20130627_140132Hongten file_name = name[0] #File extension: .jpg file_ext = name[1] print('Full name of document:{0},filename:{1},File Suffix:{2}'.format(file_full_name,file_name,file_ext)) print('#' * 40) #Creating empty folders #('E:\\mydir') # Create multi-level catalogs #(r'E:\\bb\\cc') print('#' * 40) # Open a file fp = open(file_abs_path,'w+') #print('Reading file: first line of {0}: {1}'.format(file_abs_path,())) # treats each line of the file as a member of a list and returns the list. it's actually done internally by calling readline() in a loop. # If the size parameter is supplied, size is an indication of the total length of the read content, i.e. only a part of the file may be read. #print('Reading file: {0} all contents: {1}'.format(file_abs_path,())) content = 'this is a test message!!\ngood boy!\ngogo......\nhello,I\'m Hongten\nwelcome to my space!' (content) () () fp = open(file_abs_path,'r+') print('Read file:{0}All content:{1}'.format(file_abs_path,()))
Running effects:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> The current workspace is:D:\Python33\workspace Files and directories under the current workspace: ['', 'test_annotation.py', 'test_class.py', 'test_exception.py', 'test_exit.py', 'test_file.py', 'test_getA.py', 'test_hello.py', 'test_import.py', 'test_input.py', 'test_loops.py', 'test_myclass.py', 'test_os.py', 'test_range.py', 'test_str.py', 'test_string.py', 'test_while.py', 'test_with.py'] ######################################## catalogsc:\testnon-existent ######################################## True ######################################## ('E:\\test', '20130627_140132Hongten.jpg') Full name of document:20130627_140132Hongten.jpg,filename:20130627_140132Hongten,File Suffix:.jpg ######################################## ######################################## Read file:E:\test\All content:['this is a test message!!\n', 'good boy!\n', 'gogo......\n', "hello,I'm Hongten\n", 'welcome to my space!'] >>>
I hope that what I have said in this article will help you in Python programming.