Python step-by-step: File system operations

Source: Internet
Author: User

Use an example to familiarize yourself with the basic operation of a file: create a file, read a file, modify a file, delete a file, rename a file, determine whether a file exists

1 " "2 Write a table of employee information that can be Queried--school number name age class3 1. Provide a format query interface4 2. Allow users to add or delete employee information5 " "6 ImportOS7Employee_dir ='employee_list'8index=employee_dir+"/index"9 Ten  one if( notos.path.exists (employee_dir)): aOs.mkdir (employee_dir,777) -  - defadd_student (id,name,age,_class): theLocal_file = employee_dir+"/"+Str (id) -     if(os.path.isfile (local_file)): -         Print("id:%s already exists, please add it again"%(ID)) -     Else: +F=open (local_file,"W") -F.write (name+"\ t"+str (age) +"\ t"+_class) + f.close () aF=open (index,"a") atF.write (str (id) +"\ t"+name+"\ n") - f.close () -         Print("id=%s Add Success"%Id) -  - defSelect_student (* *kwargs): -      forKeyinchKwargs.keys (): in         if(key=='ID'): -Local_file = employee_dir+"/"+Str (kwargs[key]) to             if( notos.path.isfile (local_file)): +                 Print("ID does not exist:", Kwargs[key]) -             Else: theF=open (local_file,"R") *                 Print(kwargs[key],f.read ()) $         elif(key=='name'):Panax Notoginseng             if( notos.path.isfile (index)): -                 Print("name does not exist:", Kwargs[key]) the             Else: +I=0 aF=open (index,"R") the                  forLineinchF.readlines (): +Id,name = Line.strip (). Split ("\ t") -                     if(name = =kwargs[key]): $I=i+1 $Select_student (id=Id) -                 if(i==0): -                     Print("name does not exist:", Kwargs[key]) the         Else: -             Print("invalid fetch method, only through ID and name to get!")Wuyi  the  - defmodify_student (id,name,age,_class): wuLocal_file = employee_dir+"/"+Str (id) -     if(os.path.isfile (local_file)): about Os.remove (local_file) $ Rm_line (index,id) - add_student (id,name,age,_class) -         Print("id=%s Modified successfully"%Id) -     Else: a         Print("id:%s does not exist, please add first"%(ID)) +  the defDelete_student (* *kwargs): -      forKeyinchKwargs.keys (): $         if(key=='ID'): theLocal_file = employee_dir+"/"+Str (kwargs[key]) the             if( notos.path.isfile (local_file)): the                 Print("ID does not exist:", kwargs[key],", you cannot delete") the             Else: - Os.remove (local_file) in Rm_line (index,kwargs[key]) the                 Print("Delete id=%s Success"%kwargs[key]) the         elif(key=='name'): about             if( notos.path.isfile (index)): the                 Print("name does not exist:", kwargs[key],", you cannot delete") the             Else: theI=0 +F=open (index,"R") -                  forLineinchF.readlines (): theId,name = Line.strip (). Split ("\ t")Bayi                     if(name = =kwargs[key]): theI=i+1 theDelete_student (id=Id) -                 if(i==0): -                     Print("name does not exist:", kwargs[key],", you cannot delete") the         Else: the             Print("Invalid delete method, only by ID and name to delete!") the  the defRm_line (file,id): -     if( notos.path.isfile (file)): the         Print("file does not exist:", File) the     Else: theF= Open (file,"R")94of = Open (file+". tmp","W") the          forLineinchF.readlines (): theLocalid,localname = Line.strip (). Split ("\ t") the             if(str (id) = =localid):98                 Pass about             Else: - Of.write (line,)101 f.close ()102 of.close ()103         if(os.path.isfile (file+". Tmpt")): Os.remove (file+". Tmpt")104Os.rename (file,file+". Tmpt") theOs.rename (file+". tmp", File)

Python step-by-step: File system operations

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.