[Tutor] Sigh first real python task

eryksun eryksun at gmail.com
Thu Sep 13 12:00:09 CEST 2012


On Wed, Sep 12, 2012 at 11:06 PM, Mike S <mikeofmany at gmail.com> wrote:

>     try:
>         ret = subprocess.call("smbclient //reportingmachine/Dashboard/; put
> %s\" -U Username%Password" % (fileName), shell=True)
>         if ret < 0:
>                 print >>sys.stderr, "Child was terminated by signal", -ret
>         else:
>             os.unlink(path+fileName)
>     except OSError, e:
>             print >>sys.stderr, "Execution failed:", e

I don't see a need to run this through the shell. I'd just use a list
of arguments.

Do you only want to delete the file if smbclient is killed by a
signal? What if it fails for some other reason with a return code of
1? In the example below I assume the file is removed only if the put
command succeeds.

>From what I gather using "man smbclient", the basic template here is
the following:

    smbclient servicename password -U username -c "put filename"

The code below uses subprocess.check_call, which raises a
CalledProcessError if the return code is non-zero. The variables
username, password, filename, and path are strings.

    import sys
    import os
    from subprocess import check_call, CalledProcessError

    servicename = "//reportingmachine/Dashboard/"

    try:
        check_call(["smbclient", servicename, password, "-U", username,
                    "-c", "put %s" % filename])

        os.unlink(os.path.join(path, filename))

    except CalledProcessError as e:
        print >>sys.stderr, "call failed:", e

    except OSError as e:
        print >>sys.stderr, "unlink failed:", e


More information about the Tutor mailing list