Example of file operation usage in python Development

Source: Internet
Author: User
This article mainly introduces the file operation usage in python development. It provides detailed analysis of Python's file path, file name, extension name, and other operation skills in the form of instances, which has some reference value, for more information, see the following example to describe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.