Example of file operation usage in python Development,
This document describes how to operate python files. We will share this with you for your reference. The details are as follows:
Let's take a look at the official API: OS-Miscellaneous operating system interfaces
Below is my demo:
Import reimport osimport 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 \ hongten.txt' # obtain the current workspace directory def getcwd (): return OS. getcwd () # obtain all files and folders in the specified folder # if the specified folder does not exist, the system returns the prompt def listdir (dir_path): if OS. path. exists (dir_path): return OS. listdir (dir_path) else: return 'Directory '+ dir_path +' does not exist 'def isfile (file_path): if OS. path. Exists (file_path): return OS. path. isfile (file_path) else: return 'file' + dir_path + 'does not exist 'if _ name _ =' _ main __': print ('current workspace is: {0 }'. format (getcwd () print ('files and directories in the current Workspace: ', listdir (getcwd () print (' # '* 40) print (listdir ('C: \ test') print ('#' * 40) print (isfile (image_path) print ('#' * 40) array = OS. path. split (image_path) print (array) # Full name of the file: 20130627_140132Hongten.jpg file_full_name = array [1] name = OS. path. splitext (file_full_name) # file name: 20130627_140132Hongten file_name = name [0] # file Suffix :. jpg file_ext = name [1] print ('file Full name: {0}, file name: {1}, file Suffix: {2 }'. format (file_full_name, file_name, file_ext) print ('#' * 40) # create an empty folder # OS. mkdir ('e: \ mydir') # create a multi-level directory # OS. makedirs (r 'e: \ bb \ CC') print ('#' * 40) # open a file fp = open (file_abs_path, 'W + ') # print ('first line of reading file: {0}: {1 }'. format (file_abs_path, fp. readline () # Each row of the file acts as a list member and returns the list. In fact, it is implemented by calling readline () cyclically. # If the size parameter is provided, size indicates the total length of the read content, that is, it may be read-only to a part of the file. # Print ('read file: {0} All content: {1} '. format (file_abs_path, fp. readlines () content = 'this is a test message !! \ Ngood boy! \ Ngogo... \ nhello, I \'m Hongten \ nwelcome to my space! 'Fp. write (content) fp. flush () fp. close () fp = open (file_abs_path, 'r + ') print ('read file: {0} All content: {1 }'. format (file_abs_path, fp. readlines ()))
Running effect:
Python 3.3.2 (v3.3.2: d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ==========================================>> the current workspace is: d: \ Python33 \ files and directories in the current workspace of the workspace: ['rename. py', '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 _. py'] ##################################### ### directory c: \ test does not exist ##################################### ### True #################################### #### ('e: \ test ', '20130627_140132Hongten.jpg') Full name: 20130627_140132Hongten.jpg, File Name: 20130627_140132Hongten, file Suffix :. jpg ####################################### ######################################## # Reading files: e: \ test \ hongten.txt all content: ['this is a test message !! \ N', 'good boy! \ N', 'gogo... \ n', "hello, I'm Hongten \ n", 'Welcome to my space! '] >>>
I hope this article will help you with Python programming.