Moving a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Memozio
    New Member
    • Sep 2007
    • 14

    Moving a file

    Hi there,
    I'm trying to figure out how to move a file from one directory to another (all C drive). I've read that shutil is the module to go with. Ok, I think "move" command is what I'm looking for. When I'm trying to run simple tests in command line python that looks like this:
    Code:
    import shutil
    move('C:\\....', 'C:\\....')
    i get an error "NameError: name 'move' is not defined"
    I've looked inside shutil.py module and there is a module "move". My guess is that I probably need to use the form like:
    Code:
     something.move('C:\\....', 'C:\\....')
    is that correct? or what's the right way to use "move" ?

    Thank you
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Memozio
    Hi there,
    I'm trying to figure out how to move a file from one directory to another (all C drive). I've read that shutil is the module to go with. Ok, I think "move" command is what I'm looking for. When I'm trying to run simple tests in command line python that looks like this:
    Code:
    import shutil
    move('C:\\....', 'C:\\....')
    i get an error "NameError: name 'move' is not defined"
    I've looked inside shutil.py module and there is a module "move". My guess is that I probably need to use the form like:
    Code:
     something.move('C:\\....', 'C:\\....')
    is that correct? or what's the right way to use "move" ?

    Thank you
    The correct usage would be:[code=Python]
    import shutil
    shutil.move(src , dst)[/code]where src is the source file or directory name and dst is the destination.

    Comment

    • Memozio
      New Member
      • Sep 2007
      • 14

      #3
      Originally posted by bvdet
      The correct usage would be:[code=Python]
      import shutil
      shutil.move(src , dst)[/code]where src is the source file or directory name and dst is the destination.
      Here we go!
      Thank you, bvdet
      Just a follow up question: So, whenever we don't have any object for "smth" in smth.method(arg , arg) then we need to use module name for left hand side?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Memozio
        Here we go!
        Thank you, bvdet
        Just a follow up question: So, whenever we don't have any object for "smth" in smth.method(arg , arg) then we need to use module name for left hand side?
        You can also do this:[code=Python]from shutil import move
        move(src, dst)[/code]I would tend to term move() a user-defined function which is a callable object created at the module level. Where are you getting 'smth'?

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by Memozio
          Here we go!
          Thank you, bvdet
          Just a follow up question: So, whenever we don't have any object for "smth" in smth.method(arg , arg) then we need to use module name for left hand side?
          "something.meth od()" syntax is used once you have created an instance of some class object. Modules may export function and/or classes and in may be a little confusing at first because object creation and function call syntax look the same. For example:
          [CODE=python]import time
          timeAsFloat = time.time() #calls a function in the time module

          import datetime
          aDateObj = datetime.date(0 ,0,0) # create a date object[/CODE]The key is to understand the interface to the module and name your variable in order to remind yourself whether it is a simple variable type or an instance of a class. I often use the ....Obj for the latter case.

          Comment

          • Memozio
            New Member
            • Sep 2007
            • 14

            #6
            Originally posted by bartonc
            "something.meth od()" syntax is used once you have created an instance of some class object. Modules may export function and/or classes and in may be a little confusing at first because object creation and function call syntax look the same. For example:
            [CODE=python]import time
            timeAsFloat = time.time() #calls a function in the time module

            import datetime
            aDateObj = datetime.date(0 ,0,0) # create a date object[/CODE]The key is to understand the interface to the module and name your variable in order to remind yourself whether it is a simple variable type or an instance of a class. I often use the ....Obj for the latter case.
            got ya,
            thx, bartonc!

            Comment

            Working...