[Tutor] Question about my reading and writing files homework

Dennis Lee Bieber wlfraed at ix.netcom.com
Sun Sep 27 12:03:09 EDT 2020


   At 12:22 PM 9/27/2020 +0000, trista rayment wrote:
    
       In future -- please make your replies to the LIST, not to the
   individual. Normally I delete any personal email that is a reply to a list
   message.  I don't know if this will show up on the list, since I am
   registered via the gmane news server interface; sending a message to
   [1]tutor at python.org may bounce.
    

      

     So I started from scratch - but now my file checks to see where it is,
     and then when I try to change the directory with chdir() - it doesn't
     change. Do you know where I would have made the mistake?

     import os

     dir_path = os.path.dirname(os.path.realpath(__file__))

     print(dir_path)

     os.chdir('F:\cpt180Stuff')

     dir_path = os.path.dirname(os.path.realpath(__file__))

     print(dir_path)

    
       __file__ is the PATH of the source file, it will never change while
   the program is running.
    
   """

os.chdir(path)
    
   Change the current working directory to path.
   """
    
       The key is "current working directory" Read the documentation looking
   for a function the tells you what the "current working directory" actually
   is.
    
   https://docs.python.org/3/library/os.html#os-file-dir
    

     folder = 'cellphones'

     path = os.path.join(dir_path, folder)

     if not os.path.exists(path):

         os.mkdir(path) 

     list = []

     for entry in os.scandir(dir_path):

         list.append(entry.name)

     print("The CPT180Stuff folder contains the following: " + str(list))

     car_path = dir_path + "\cars"

    
       car_path = os.path.join(dir_path, "cars")
    
       However, see my comment below
    

     cars = []

     for entry in os.scandir(car_path):

         cars.append(entry.name)

     print("The CPT180Stuff\cars folder contains the following: " +
     str(cars))

    
       You are again hard-coding the subdirectories, which only works if you
   already KNOW the directories. I'm quite certain your assignment is that
   you should be examining the entries from the previous os.scandir() to find
   out what directories are present, and then process those directories,
   regardless of the name.
    
    
    
    
   -- 
       Wulfraed                 Dennis Lee Bieber         AF6VN
       wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

References

   Visible links
   1. mailto:tutor at python.org


More information about the Tutor mailing list