[Tutor] Reading and Writing files homework
trista rayment
tristarayment at yahoo.com
Fri Sep 25 20:11:52 EDT 2020
I need a certain part of my code to be sorted alphabetically. It should say ['cars', 'cellphones', 'cpt180.txt', 'pets') instead of ['cpt180.txt', 'cars', 'pets', 'cellphones'] in the output, from this bit of code: print('The CPT180Stuff folder contains the following: ', os.listdir('F:\CPT180Stuff'))
How would I make that happen?
>>>os.pathisfile('F:\\cellphones')>>>os.makedirs('F:\CPT180Stuff\cellphones')and for this part - it's supposed to check to see if that file exists and if it doesn't it should create it. And I know I have it coded incorrectly.
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.'
Any help would be appreciated. Thank you!!
My Code:
>>>from pathlib import Path>>>import os>>>print(Path.cwd())>>>os.chdir('F:\CPT180Stuff')>>>print(Path.cwd())>>>os.pathisfile('F:\\cellphones')>>>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 Output:
>>>F:\CPT-180\chapter 5 assignment>>>F:\CPT180Stuff>>>The 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 bytes>>>cats.jpg 39139 bytes>>>The CPT180Stuff\pets\dogs folder contains the following: >>>dognames.txt 34 bytes>>>dogs.jpg 59520 bytes
More information about the Tutor
mailing list