#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    New to Python /Easy Quesion About Changing Script to Function


    Hello,

    I'm new to python, and as I learn my ways I came across a small....
    I have this script that runs fine as long as I don't include def....:
    Code:
    import os
    
    #doesn't delete pics if I include def...
    #def delPicts():
         dirPath= "/home/Pictures/"
         fileList= os.listdir(dirPath)
         for fileName in fileList:
             os.remove(dirPath+"/"+fileName)
    I'm trying to turn this code into a function, so I can call it from a different file to delete pictures. On the main file I use
    Code:
    from delpics import delPicts
    When I add "def delPicts():" and run this file, it doesn't delete the pictures (but no errors either).
    Any suggestions will be very appreciated.

    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Originally Posted by simply_me
    When I add "def delPicts():" and run this file, it doesn't delete the pictures (but no errors either).
    Do you ever call the function?
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Hey thanks for the reply.

    When I just run this file, and it doesn't delete anything.
    The same goes on the main file I call it:
    Code:
    dePicts()
    After importing delPicts as mentioned above, yet the same result.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    When python imports a module it executes all statements with 0 indentation.

    def at the left margin means "compile the code defined in the indented block and store the op-codes with given name". That action must happen before python invokes that code. You need this:
    Code:
    import os
    
    def delPicts():
        dirPath= "/home/Pictures/"
        fileList= os.listdir(dirPath)
        for fileName in fileList:
            os.remove(dirPath+"/"+fileName)
    
    delPicts()  # By "call the function" SuperOscar means this.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    I don't have Linux, but it works on Windows7. Test your module itself ...
    Code:
    # save this module as delPicts.py
    
    import os
    
    def delPicts():
        # Windows7 auto-assumes C:/home/Pictures/
        dirPath= "/home/Pictures/"
        fileList= os.listdir(dirPath)
        for fileName in fileList:
            os.remove(dirPath+"/"+fileName)
            print(dirPath+"/"+fileName)  # test
    
    # test the module
    # this code block will be ignored if imported as a module
    if __name__ == '__main__':
        delPicts()
    Import the module and call it's function this way ...
    Code:
    ''' delPicts_test.py
    test module delPicts.py 
    '''
    
    import delPicts
    
    # call the module's function
    delPicts.delPicts()
    Note, I prefer to use namespace notation so I know where a function came from. Nice, when you write large code.
    Last edited by Dietrich; February 10th, 2013 at 09:27 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo