Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

more python crap ..

Options
  • 15-10-2008 1: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 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 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 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 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