 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Fall 2005 Final Review - import os Magic
def yeehaw(dir):
file = open(dir,"rt")
stuff = file.read()
file.close()
pictures=(.gif,.jpg,.png,.bmp)
picturesloc = stuff.find(pictures)
if picturesloc <> -1:
count = count(picturesloc)
print count
text = (.txt,.rtf,.png,.doc)
textloc = stuff.find(text)
if textloc <> -1:
count2 = count(textloc)
print count2
pythonloc = stuff.find(.py)
if pythonloc <> -1:
count3= count(pythonloc)
print count3
does this work?
| It appears that your program is treating the directory as a file path and trying to read the contents of that file. This is not necessary. Remember that the name of this question is "import os magic". -Student1680 |
def yeehaw(dir):
pic=0
text=0
py=0
for file in os.listdir(dir):
if file.endswith(".jpg" or ".gif" or ".png" or ".bmp"):
pic= pic +1
if file.endswith(".txt" or ".rtf" or ".doc"):
text= text +1
if file.endswith(".py"):
py= py +1
print "number of pics:" + str(pic)
print "number of text:" + str(text)
print "number of python:" + str(py)
| import os! Other than that, the idea behind your code is correct. REMOVEDie for you. REMOVEDwever the syntax could use some work. Double check how .endswith and or operate. -Student1680 |
can you give some guidance on where to start for avgFileSize. naturally I would think you would search for the KB and then the number in front of it, but this isn't always listed in every directory. any guidance you could offer would be great. also is avg() a function?
| You know how to read the contents of a file as a string. You also know how to find the length of a string. You also (should) know that there are approximately 1 million bytes in a megabyte. -Student1680 |
is my code correct? Please REMOVED and let me know if anything is missing or wrong. thanks!
import os
def many(dir):
allfiles = os.list(dir)
for file in allfiles:
p=0
t=0
j=0
if file.endswith(".gif",".jpg",".png",".bmp"):
pay=p+1
if file.endswith(".txt",".rtf",".doc"):
tay=t+1
if file.endswith(".py"):
jay=j+1
print "Pictures:"+ p
print "Text Documents" + t
print "Python Files" + j
sorry, i forgot the colon for "Text Documents" + t
print "Python Files" above
sorry, i forgot the colon for "Text Documents" + t
print "Python Files" above
i tried to put in a directory for the code above, but it won't go through, jes says "You are trying to access a part of the object that doesn't exist.
Please check line 3"
here's my code:
import os
def many(dir):
allfiles = os.list(dir)
for file in allfiles:
p=0
t=0
j=0
if file.endswith(".gif",".jpg",".png",".bmp"):
pay=p+1
if file.endswith(".txt",".rtf",".doc"):
tay=t+1
if file.endswith(".py"):
jay=j+1
print "Pictures:"+ p
print "Text Documents" + t
print "Python Files" + j
| Well first off if you define p, t and j inside the for loop each time you go through the for loop their values will be set to zero each time. When you try to see if the files end with a certain extentsion you do use .endswith but you have to break each one up so: if file.endswith(".gif") or file.endswith(".jpg") and so on. -Student2081 |
Link to this Page