Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2013, 03:52 AM
simply_me simply_me is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 simply_me User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 22 sec
Reputation 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

Reply With Quote
  #2  
Old February 10th, 2013, 04:02 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 7 h 25 m 55 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old February 10th, 2013, 04:08 AM
simply_me simply_me is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 simply_me User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 22 sec
Reputation 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.

Reply With Quote
  #4  
Old February 10th, 2013, 05:37 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
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!

Reply With Quote
  #5  
Old February 10th, 2013, 09:12 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 499 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 25 m 30 sec
Reputation Power: 63
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.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : February 10th, 2013 at 09:27 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > New to Python /Easy Quesion About Changing Script to Function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap