Python-based files, directories

Source: Internet
Author: User

1. File operation.

(1) file creation.

(2) file read.

(3) File rename.

(4) File deletion.

2. Directory operations.

(1) Directory creation.

(2) directory deletion.

he.py

#Coding=utf8ImportOS#Create a filedefWriteFile (FILENAME,STR): FS=open (FileName,"w+") fs.write (str) fs.close ()Print("file"+filename+"created successfully")#Read File contentsdefReadFile (fileName): FS=open (FileName,"r+") Str=Fs.read ()Print("File Contents:"+str)#Deleting Filesdefremovefile (filename): os.remove (filename)Print("file"+filename+"Delete succeeded")#file RenamedefRenameFile (oldfilename,newfilename): Os.rename (oldfilename,newfilename)Print("file"+oldfilename+"Change Name Success")#Create a directorydefCreatedir (dirName): Os.mkdir (dirName)Print("file directory"+dirname+"created successfully")#Delete directorydefRemoveDir (dirName): Os.rmdir (dirName)Print("file directory"+dirname+"Delete succeeded")

Calling code:

Python 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> fromHeImport*>>> WriteFile ("a.txt","Hello Tom Welcome") file A.txt created successfully>>> ReadFile ("a.txt","B.txt") Traceback (most recent): File"<pyshell#2>", Line 1,inch<module>ReadFile ("a.txt","B.txt") Typeerror:readfile () takes1 positional argument but 2were given>>> ReadFile ("a.txt") file contents: Hello Tom Welcome>>> RenameFile ("a.txt","B.txt") file A.txt change name succeeded>>> RemoveFile ("B.txt") file B.txt deleted successfully>>> Createdir ("AA") file directory AA created successfully>>> RemoveDir ("AA") file directory AA Delete succeeded>>>

Python-based files, directories

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.