You should be using
import requests
import os
url = 'http://someplace'
folder = 'path/to/folder/'
for card in os.listdir(folder):
data = {'identifier': '.'.join(card.split('.')[:-1]), 'type': 'Inventory Card'}
files = {'card': open(os.path.join(folder, card), 'rb')}
requests.post(url, data=data, files=files, auth=('username', 'pass'))
'.'.join(card.split('.')[:-1])
takes the filename (in card
), splits it into components that were separated by a period in the file name, rejects the last element of the list [:-1]
and rejoins the filename minus the extension.
Using this syntax will handle filenames with multiple periods such as foo.20120613.txt
EDIT:
An example that lists files in my ~/tmp
directory...
[mpenning@Bucksnort tmp]$ ls
howto regex_trial.xml soln.py
[mpenning@Bucksnort tmp]$
[mpenning@Bucksnort tmp]$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for card in os.listdir('./'):
... print "card:", card
... print " base_name:", '.'.join(card.split('.')[:-1])
...
card: howto
base_name:
card: regex_trial.xml
base_name: regex_trial
card: soln.py
base_name: soln
>>>
Note that howto
is a directory... you need to put logic in your script to skip a directory if there is one in this path.