Python- output to term & file

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:


#!/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

Thanks all.

This will not anser your question, but to avoid misunderstandings: tee is not a bash feature or internal command. It is jut a tool:

henk@boven:~> which tee
/usr/bin/tee
henk@boven:~>

As such you can use the command wherever you can use commands. But I admit that it is most usefull in a pipeline, which you most often use/program in a shell. But not just in bash, in any shell.

On 2014-06-25, sparkz alot <sparkz_alot@no-mx.forums.opensuse.org> wrote:
> In my quest to decide which language I like best, my nephew suggested
> Python.

Stop right there. What do you mean by which language I like best'? Best for what? I routinely use Python, C++, x86/x86_64 assembler, but I would never claim any one of these is the best’. It depends on the task. For high-level
operations, Python does very well, but there are really no compelling reasons (other than religious) to choose it in
general over closely-related competitors such as Ruby. Sure, there are niche arguments one way or another, but then
you’ve already defined your desired task.

> I found a cool command ‘fdupes’ that I thought would be nice to
> build a simple project around.

IIRC fdupes is written in C. It’s therefore not an ideal starting point to help you decide on a high-level language
such as Python. The chances are that any program you write based on fdupes will be more efficiently written in bash.

> Here is what I have so far:

I cannot interpret your Python code with 100% confidence because the openSUSE forum does not respect blank space
indentation, which of course is syntactically important. From what I can make out, your code won’t work because you’re
mixing Python code with a bash coded embedded within a separate subprocess. Since I don’t know what you’re trying to do
with the code, I can’t help much beyond that. I can identify things that `just don’t do anything useful’, such as:


....
f=open("/home/carissa/dupedir.txt","a+")
....
f.close()
....

Between these two lines the variable `f’ is never used, and therefore these lines effectively do nothing.

But if you’re learning a language, start with something the language was designed to do. I guess I could write a clumsy
Python interface for fdupes if I really wanted to (and necessarily calling fdupes using a bash subprocess) but even if I
wanted to do, I certainly wouldn’t start learning Python that way.

Thanks flymail for the response. To clarify, when my nephew recommended Python as ‘best’, he meant ‘best’ for me, not necessarily the ‘best’ language out there. Being new to the programming world, I wanted something a bit more flexible than bash. I initially tried c++, but to be honest, it was too complicated for me to grasp :). His thinking was that Python would be easier to learn and once learned, would make other languages like c++ easier to understand. Since starting this post, I’ve started taking MIT’s Open Course on beginning computer science, which uses Python 2.7, probably something I should have done from the start :).

I should point out, that I am also new to Linux and on occasion run across a command like ‘fdupes’ , ‘rsync’, etc. that just strike me as, well cool. Things that aren’t available or well hidden in windows. I admit, when it comes to Linux, I feel like a kid in a candy shop.

As to the code:


....
f=open("/home/carissa/dupedir.txt","a+")
 ....
 f.close()
 ..../CODE]
you're right, it basically does nothing, other than create a file if it doesn't already exist  or append it if it does. My problem was to get the program to print to the file **and** the terminal at the same time, like I could with Bash. Perhaps by the end of this class I will learn how to do that.

Hopefully, someday I will be able to give the same expert advise offered by you and the other members of the forum, till then, I will be content being a thankful recipient.

Again, thanks

On 2014-07-14, sparkz alot <sparkz_alot@no-mx.forums.opensuse.org> wrote:
> Hopefully, someday I will be able to give the same expert advise offered by you and the other members of the forum,
> till then, I will be content being a thankful recipient.
>
> Again, thanks

Pleasure! I think your nephew is quite right to suggest it’s better to start with Python before C++. It’s one thing to
learn how to program, and another thing (once you know how to program) to master a different programming language. Once
you’ve selected a first language, stick with it. And I’m in 100% agreement with your nephew that Python is the `best’
place to start. There are many reasons why Python was chosed as the preferred learning language for the Raspberry Pi and
so I’m not the only one agreeing with your nephew!

Bear in mind that Python is very different from bash. Although technically speaking, both are scripting languages,
they are designed with very different philosophies. I can see that Linux has whetted your appetite with bash scripting
and you are looking to extend your skills. For now, I recommend you treat Python and bash as separate entities. Remember
Python also works in other operating systems (including Windows), that don’t necessarily include bash. I hope your MIT
course is going well, but if you’re starting to program for the first time and have chosen Python as your first
language, then you might want to bookmark a link to Zed Shaw’s book: http://learnpythonthehardway.org/book/

Appreciate the link and I did indeed bookmark it.