In my quest to decide which language I like best, my nephew suggested Python. I found a cool command 'fdupes' that I thought would be nice to build a simple project around. I have however, run into a problem that I just can't find an answer to and that is, how can I direct the output to both the terminal and to a file at the same time (like bash's 'tee'? Also, I'm not sure, but I don't think my "while" statement is working correctly when I use a variable in 'myCmd' rather than using the actual file location.
Here is what I have so far:
Thanks all.
Here is what I have so far:
Code:
#!/usr/bin/python
#
# File name: fsdupes.py
# Written by sparkz_alot
# 24 Jun 2014
# Description: A program written in Python 2.7 to check for duplicate files
#
name = raw_input('Enter Name: ')
print "How's it going ", name
#
# Lets prepare for determining our runtime
import datetime
startdt = datetime.datetime.now()
print "Start time : ", startdt
#
# Open file with (a)ppend, (r)ead and (w)rite permission. Creates new file if one doesn't exist.
f=open("/home/carissa/dupedir.txt","a+")
#
# Write command output to file
import subprocess
import sys
#
# Which directory do we want to check
# TODO: make this user defined
dupedir = "/home/carissa"
myCmd = "/usr/bin/fdupes -r " + dupedir
#
# run command
#
myOut = subprocess.Popen(myCmd, shell=True, stderr=subprocess.PIPE)
#
# Lets not wait, rather start displaying immediatly
#
while True:
out = myOut.stderr.read(1)
if out == '' and myOut.poll() != None:
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()
#
# Close the file
f.close()
#
enddt = datetime.datetime.now()
print "End time: ", enddt
diff_sec = enddt - startdt
#
print "Total runtime: ", diff_sec
#
# TODO Work on this
#
numOfDupes="0"
numOfFD="0"
print "Count of Duplicates: " + numOfDupes
print "Count of Files/Directories: " + numOfFD
#
# User control of ending program. Comment out if not needed
#
raw_input("Press <enter> to continue...")
#
# See also fslint