Python file Operation review A

Source: Internet
Author: User

Review file operations
Print "Current path:" Print os.getcwd () print "Determines whether it is a file:" Print Os.path.isfile (OS.GETCWD ()) print "is is a directory: "Print Os.path.isdir (OS.GETCWD ()) Print os.system (" ls ") print" Determines if it is an absolute path: "Print Os.pat H.isabs ("excp01.py") print "verifies that the given path is really saved:" Print os.path.exists ("/users/zhouhaijun") print "returns a directory for a path        Name and file name "Print os.path.split ("/users/zhouhaijun ") print" Detach file name with extension "Print os.path.splitext (" Tmp1.dat ") Print "Finds all the files in a directory and writes" Ostest "for Root,dis,files in Os.walk ("/users/zhouhaijun/desktop/hhhhh/ostest "): For name in Files:print Name,root,os.path.join (Root,name) with open (Os.path. Join (Root,name), "W") as Fp:fp.write ("good!        Liwen \ n Learn Walk "command") print "ok!" Print "If the file name in a directory contains a DAT suffix name, append a line to it" I found it!  "For Root,dirs,files in Os.walk ("/users/zhouhaijun/desktop/hhhhh/ostest "):          For name in Files:suffix = Os.path.splitext (Os.path.join (Root,name)) [-1] if suffi                        X in "Dat": the Print name with open (Os.path.join (root,name), "a") as FP: Fp.write ("\ n i found it!!! ") print" Modified: "Os.system (" Cd/users/zhouhaijun/desktop/hhhhh/ostest ") print" Get Current path "Os.chdi R ("/users/zhouhaijun/desktop/hhhhh/ostest") print "Get current directory:" Print OS.GETCWD () for line in FILEINPUT.I Nput ("dd", inplace = 1): line = Line.replace ("Walk", "\ n review fileinput.input command") Print line print            "Seek function:" With open ("DD", "r+") as F:old = F.read () print "content is:", Old F.seek (2) F.write ("Liwen is very GOOGD!!!!")
Results:

Current path:
/users/zhouhaijun/pycharmprojects/test/pythontest/dome01
Determine if it is a file:
False
is a directory:
True
monday.py
init. py
Contect_list.txt
excp01.py
one_01.py
Phone.dat
Test.kpl
Tmp1.dat
Tmp2.dat
0
Determine if it is an absolute path:
False
Verify that the given path is really saved:
True
Returns the directory name and file name of a path
('/users ', ' Zhouhaijun ')
Detach file name and extension
(' Tmp1 ', '. dat ')
Find all the files in a directory and write "Ostest" to each file
Dd/users/zhouhaijun/desktop/hhhhh/ostest/users/zhouhaijun/desktop/hhhhh/ostest/dd
Liwen.dat/users/zhouhaijun/desktop/hhhhh/ostest/users/zhouhaijun/desktop/hhhhh/ostest/liwen.dat
Ok!
If the file name in a directory contains a DAT suffix name, append a line to it "I found it!"
Dd
What to modify:
Get current path
Get current directory:
/users/zhouhaijun/desktop/hhhhh/ostest
Seek function:
Content is: good! Liwen

Learn
Review fileinput.input command commands

I found it!!!

Python file Operation review A

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.