Search code examples
pythonpython-requestsglob

Can't print the result of POSTing all files in a folder in Python?


I have a Python script that tries to POST all the files in a certain directory with the extension ".csv" to a URL, and then print the result (using Requests and Glob):

import requests, glob


text_files = glob.iglob("./user/Documents/folder/folder/*.csv")


url = 'http://myWebsite.com/extension/extension/extension'

for data in text_files:
    current_file = open(data)
    r = requests.post(url, files=current_file)
    print r.text

However, nothing is printed, even though POSTing the same files in Terminal via cURL produces an output specific to the server. I can't figure out why but am guessing that I'm somehow implementing glob incorrectly.


Solution

  • Fixed via trial and error. I apologize if I incorrectly answer my own question (as far as etiquette is concerned, that is) and will try to give credit where it is necessary. Credit to @Martijn Pieters for suggesting that something was wrong with the path to the directory. Went to where the .csv files are and got the path to them manually via "get info" (I'm on a MAC). The correct path should have been:

    /Users/ME/Documents/folder/folder/*.csv
    

    Also current_file = open(data) should be replaced with current_file = {'file': open(data)} The problem here is that Python doesn't know that "data" is a file and will throw a plethora of errors. New code:

    import requests, glob
    
    text_files = glob.iglob("/Users/ME/Documents/folder/folder/*.csv")
    
    url = 'http://myWebsite.com/extension/extension/extension'
    
    for data in text_files:
        with open(data) as f:
            file2 = {'file': f}
            r = requests.post(url, files=file2)
        print r.text