Python 3.x Study Notes 8 (OS module and xml modification), python3.x

Source: Internet
Author: User

Python 3.x Study Notes 8 (OS module and xml modification), python3.x

1. OS module operations

OS. getcwd (): # view the current path.

OS. listdir (path): # lists all objects in the directory and returns the list type.

OS. path. abspath (path): # returns the absolute path of path.

OS. path. join (path1, path2,...): # combines paths. If there is an absolute path, the previous path will be deleted.

OS. path. dirname (path): # Return the folder section in path. The result does not contain '\'

OS. path. basename (path): # Return the file name in path.

OS. path. getmtime (path): # The last modification time of the file or folder, from the new era to the access time.

OS. path. getatime (path): # The last access time of the file or folder, from the new era to the access time in seconds.

OS. path. getctime (path): # the time when a file or folder is created, from the new era to the access time.

OS. path. getsize (path): # size of the file or folder. If it is a folder, 0 is returned.

OS. path. exists (path): # Check whether the file or folder exists. True or False is returned.

 

2. Use of xml

Xml Creation

From xml. etree import ElementTree as ETdef build_sitemap (): urlset = ET. element ("urlset") # ET. element creates a root node with the label urlset url = ET. subElement (urlset, "url") # ET. subElement creates the sub-node loc = ET under the root node urlset. subElement (url, "loc", attrib = {"name": "Baidu"}) # attrib creates the attribute loc. text = "http: // www/baidu.com" # loc. time = ET. subElement (url, "time") time. text = "8-8-1-30" change = ET. subElement (url, "change") change. text = "daily" priority = ET. subElement (url, "priority") priority. text = "1.0" tree = ET. elementTree (urlset) tree. write ("set. xml ", 'utf-8') # When writing, you can add 'utf-8' to translate Chinese characters, if _ name _ = '_ main _': build_sitemap ()

Generated xml

<Urlset> <url> <loc name = "Baidu"> http: // www/baidu.com </loc> <time> </time> <change> daily </change> <priority> 1.0 </priority> </url> </urlset>

The files to be modified are as follows:

<?xml version="1.0"?><data>    <country name="Liechtenstein">        <rank>1</rank>        <year>2008</year>        <gdppc>141100</gdppc>        <neighbor name="Austria" direction="E"/>        <neighbor name="Switzerland" direction="W"/>    </country>    <country name="Singapore">        <rank>4</rank>        <year>2011</year>        <gdppc>59900</gdppc>        <neighbor name="Malaysia" direction="N"/>    </country>    <country name="Panama">        <rank>68</rank>        <year>2011</year>        <gdppc>13600</gdppc>        <neighbor name="Costa Rica" direction="W"/>        <neighbor name="Colombia" direction="E"/>    </country></data>

Modify Program

Import xml. etree. elementTree as ETtree = ET. parse ('xmltest. xml ') root = tree. getroot () # modify for node in root. iter ('Year'): new_year = int (node. text) + 1 node. text = str (new_year) node. set ('updated _ by', 'hsj') tree. write ('xmltest2. xml ') # delete for country in root. findall ('country'): rank = int (country. find ('rank '). text) if rank> 50: root. remove (country) tree. write ('xmltest3. xml ')

 

Related Article

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.