Reputation: 31
I have a problem here
I'm trying to create a program that can remove pyc files. The problem is every time I run the program through cmd, always appears a message like this:
windows error: [error 2] the system cannot find the file specified: "blabla.pyc"
And, this is the code:
directory = "C:\\Documents and Settings\\baba\\My Documents"
try:
for root, dirs, files in os.walk(directory):
for file in fnmatch.filter(files, p):
print "mencari... ",root
size = os.path.getsize(file)
dateTime = time.ctime(os.path.getmtime(file))
dateTuple = time.localtime(os.path.getmtime(file))
dateFile = dateTuple[0:3]
todayTuple = time.localtime(time.time())
todayFile = todayTuple[0:3]
if size == 0:
print "\n\n\t File name %s size %d bytes \n" %(file, size)
choice()
I know, here so many have asked this. But, every time I tried, no one has managed one.
Oh, every time I put a comment from size variable to bottom, the program can run smoothly. But, when I do not comment, the issue resurfaced.
There must be something wrong with my logic. Can anyone make a suggestion? I really do not know what to do anymore.
thank you for your answer
Upvotes: 0
Views: 1767
Reputation: 9696
More or less guessing:
Try replacing file
with os.path.join(root, file)
:
absfile = os.path.join(root, file)
size = os.path.getsize(absfile)
dateTime = time.ctime(os.path.getmtime(absfile))
dateTuple = time.localtime(os.path.getmtime(absfile))
files
as returned by os.walk
returns a list of filenames - not full paths:
E.g. "blabla.pyc"
, while you would need "C:\\Documents and Settings\\baba\\My Documents\blabla.pyc"
So if you're not running this script from the directory
, getmtime
etc will fail.
Upvotes: 2