1

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?

joshdcomp
  • 1,618
  • 3
  • 19
  • 26
  • 2
    Looks like you are mixing Python and the Python C API; the latter is only needed when coding Python extensions in C.. – Martijn Pieters Jun 15 '12 at 10:19
  • 2
    What did you think `PyFile_Name(PyObject *p)` would do? – Fred Foo Jun 15 '12 at 10:23
  • @iarsmans I found it [here](http://docs.python.org/c-api/file.html?highlight=file_name#PyFile_Name)…the way they described it, it sounded like it would return the filename without the extension. I don't know what *p would do though…maybe it's a Python variable (similar to (e) in Javascript. Would you suggest an alternative? – joshdcomp Jun 15 '12 at 10:33
  • I think you are misunderstanding what *p means in the function definition. See here for an explanation of args and kwargs: http://stackoverflow.com/questions/3394835/args-and-kwargs – FakeDIY Jun 15 '12 at 10:55
  • Do you know what the `c-api` part of the URL is referring to? How did you get there? – Karl Knechtel Jun 15 '12 at 12:22
  • Also, what other languages have you coded in before? – Karl Knechtel Jun 15 '12 at 12:24
  • @KarlKnechtel I code in PHP/Javascript mostly—so Python is getting to the edge of my comfort level. I'm not sure what you mean by the `c-api`(@Martijn Pieters mentioned this too) part of the URL…this is a mix of code I got from the dev that built the API and stumbling through reference files. – joshdcomp Jun 15 '12 at 15:16

1 Answers1

1

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.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • I gave this a shot…and it didn't seem like it worked. I'm not sure if it was the API (still in development) or if it was the code…I'll keep testing. Could I save `'.'.join(card.split('.')[:-1])` as a variable and pass that as an argument to `data`? – joshdcomp Jun 15 '12 at 16:07
  • yes you can save the information to a temporary variable... also see my edit above to show an explicit example of how this works... – Mike Pennington Jun 15 '12 at 16:14
  • Your first example worked wonderfully. The API wasn't configured to handle 'identifier' yet, so I was banging my head against a wall. It worked once I changed to another data attribute. – joshdcomp Jun 16 '12 at 06:50