[Tutor] Question about my reading and writing files homework

trista rayment tristarayment at yahoo.com
Fri Sep 25 13:37:13 EDT 2020


I'm having an issue with one thing, and I have a question about another thing. 


So first, the question does it have to say 'pathlib'? (would that mean path library?) can I substitute other words here?
My issue is that I need the bolded part to be sorted alphabetically. It should say ['cars', 'cellphones', 'cpt180.txt', 'pets')how would I make that happen?
Any help you guys could give would be greatly appreciated. I've been trying to figure this out for a while and I feel like I'm getting further away from the answer rather than close to it.
my output:
F:\CPT-180\chapter 5 assignmentF:\CPT180StuffThe CPT180Stuff folder contains the following:  ['cpt180.txt', 'cars', 'pets', 'cellphones']The CPT180Stuff\cars folder contains the following:  ['familycars', 'sportscars']The CPT180Stuff\pets folder contains the following:  ['cats', 'dogs']The CPT180Stuff\pets\cats folder contains the following: catnames.txt 34 bytescats.jpg 39139 bytesThe CPT180Stuff\pets\dogs folder contains the following: dognames.txt 34 bytesdogs.jpg 59520 bytes

my code:
from pathlib import Pathimport osprint(Path.cwd())os.chdir('F:\CPT180Stuff')print(Path.cwd())os.makedirs('F:\CPT180Stuff\cellphones')print('The CPT180Stuff folder contains the following: ', os.listdir('F:\CPT180Stuff'))print('The CPT180Stuff\cars folder contains the following: ', os.listdir('F:\CPT180Stuff\cars'))print('The CPT180Stuff\pets folder contains the following: ', os.listdir('F:\CPT180Stuff\pets'))print('The CPT180Stuff\pets\cats folder contains the following: ')print('catnames.txt: ', os.path.getsize('F:\CPT180Stuff\pets\cats\catnames.txt'), 'bytes')print('cats.jpg: ', os.path.getsize('F:\CPT180Stuff\pets\cats\cats.jpg'), 'bytes')print('The CPT180Stuff\pets\dogs folder contains the following: ')print('dognames.txt: ', os.path.getsize('F:\CPT180Stuff\pets\dogs\dognames.txt'), 'bytes')print('dogs.jpg: ', os.path.getsize('F:\CPT180Stuff\pets\dogs\dogs.jpg'), 'bytes')

my teacher also sent us some hints - and I have no idea if I'm using any of them correctly or not:
once you change to the CPT180Stuff directory, use relative path names.  That means, you will use paths like 'pets' or 'pets/cats'.  Note that these paths do not start with a '/' since they are relative to the current directory.  Also, they do not include CPT108Stuff in the name.  This makes it easier to move your script from one location to another.  When you print the contents of the directories, don't assume that they look exactly like what is downloaded with the program.  There may be more files; don't assume there are only two.  When you are getting the size, remember you must add the path to the name of the file.  For you programming students especially, I suggest you put the path in a directory and use it in both the for-loop and the call to get the size.






More information about the Tutor mailing list