Let me start by saying that I’ve never coded in Python.
I need to make an API call to upload a bunch of files from my local machine to a server. On the server, I want to take the file name of the files and assign the string sans the extension to a variable called ‘identifier.’
import requests
import os
url = 'http://someplace'
folder = 'path/to/folder/'
for card in os.listdir(folder):
data = {'identifier': PyFile_Name(PyObject *p), 'type': 'Inventory Card'}
files = {'card': open(os.path.join(folder, card), 'rb')}
requests.post(url, data=data, files=files, auth=('username', 'pass'))
Am I doing this right?
You should be using
'.'.join(card.split('.')[:-1])takes the filename (incard), 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.txtEDIT:
An example that lists files in my
~/tmpdirectory…Note that
howtois a directory… you need to put logic in your script to skip a directory if there is one in this path.