Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

more python crap ..

  • 15-10-2008 01:34PM
    #1
    Closed Accounts Posts: 1,788 ✭✭✭


    Hi I have this code
        for data in e.walk():
                 name = data.get_filename()
                 print type(name)
                 if type(name)== type(''):
                    print name
                    fileobject = open(name, 'w') 
                    fileobject.write(data.get_payload(decode = 1))
                    fileobject.close()
    

    When i am going through an email and saving the attachment, it saves the attachment by default at
    the same folder that the python file is in .. how can i change that ?, how can i specify the full or relative path ?


Comments

  • Registered Users, Registered Users 2 Posts: 21,611 ✭✭✭✭Sam Vimes


    fileobject = open("path/you/want/" + name, 'w')

    should do it


  • Closed Accounts Posts: 1,788 ✭✭✭jackdaw


    Thanks .. I just happened to try that ..


  • Closed Accounts Posts: 1,788 ✭✭✭jackdaw


    if you are referring to a file in python you can obviously say full path




    but if i wanna refer to all the files (or .html files) in the above folder ,,

    none of the following work







    It gives me an invalid file error ?


  • Registered Users, Registered Users 2 Posts: 85 ✭✭slavigo


    I'm not fully sure what your after but if your looking to get all html files in a given directory then the following will do the trick.

    In the "os" module you can use the "listdir" function for all the contents of a directory.
    You can then filter the findings using the "splitext" function in the "os.path" module.

    import os
    
    directory_path = "/YOUR/PATH/HERE"
    
    directory_contents = os.listdir(directory_path)
    
    directory_contents_filtered = []
    
    for item in directory_contents:
        if os.path.splitext(item)[1] == '.html':
            item = os.path.join(directory_path,item)
            directory_contents_filtered.append(item)
    
    print directory_contents_filtered
    


    Or short version:

    import os
    
    directory_path = "YOUR/PATH/HERE"
    
    directory_contents_filtered = [os.path.join(directory_path,item) for item in os.listdir(directory_path) if os.path.splitext(item)[1] == '.html']
    
    print directory_contents_filtered
    


    Another alternative is to use the "glob" module

    import glob
    
    directory_contents_filtered = glob.glob('/YOUR/PATH/HERE/*.html')
    
    print directory_contents_filtered
    


    Hope these help, if I've missed what your after. Sure give a little more detail and I'll see what I can do.


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Since when is python "crap"?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 85 ✭✭slavigo


    Cantab. wrote: »
    Since when is python "crap"?

    I have to admit that that was my initial reaction but then I got distracted answering.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    He wasn't suggesting the python was crap, the only person who seems to suggest that about programming languages is you Cantab. If you want to have a row then go to the pub buy someone a pint and discuss the budget. Stop looking for one in here.


Advertisement