1

I've got a function meant to download a file from a URL and write it to a disk, along with imposing a particular file extension. At present, it looks something like this:

import requests
import os
def getpml(url,filename):
 psc = requests.get(url)
 outfile = os.path.join(os.getcwd(),filename+'.pml')
 f = open(outfile,'w')
 f.write(psc.content)
 f.close()
 try:
 with open(outfile) as f:
 print "File Successfully Written"
 except IOError as e:
 print "I/O Error, File Not Written"
 return

When I try something like

getpml('http://www.mysite.com/data.txt','download') I get the appropriate file sitting in the current working directory, download.pml. But when I feed the function the same arguments without the ' symbol, Python says something to the effect of "NameError: name 'download' is not defined" (the URL produces a syntax error). This even occurs if, within the function itself, I use str(filename) or things like that.

I'd prefer not to have to input the arguments of the function in with quote characters - it just makes entering URLs and the like slightly more difficult. Any ideas? I presume there is a simple way to do this, but my Python skills are spotty.

asked Jan 3, 2013 at 1:45

2 Answers 2

4

No, that cannot be done. When you are typing Python source code you have to type quotes around strings. Otherwise Python can't tell where the string begins and ends.

It seems like you have a more general misunderstanding too. Calling getpml(http://www.mysite.com) without quotes isn't calling it with "the same argument without quotes". There simply isn't any argument there at all. It's not like there are "arguments with quotes" and "arguments without quotes". Python isn't like speaking a natural human language where you can make any sound and it's up to the listener to figure out what you mean. Python code can only be made up of certain building blocks (object names, strings, operators, etc.), and URLs aren't one of those.

answered Jan 3, 2013 at 1:47
Sign up to request clarification or add additional context in comments.

3 Comments

Is it bad that I might be a little gratified to find something where the answer is "No, that doesn't work" rather than "Umm...there's a built-in standard library function for that"?
@EpiGrad: Heh, a rarity in Python! :-)
I guess it's the same (not able to pass non-enclosed string into a function) for every languages...The reason is the same as @BrenBarn said, by saying "string" you already conform to the notion of <something enclosed by quotes> (from the language's definition) so yeah, it's not possible.
2

You can call your function differently:

data = """\
http://www.mysite.com/data1.txt download1
http://www.mysite.com/data2.txt download2
http://www.mysite.com/data3.txt download3
"""
for line in data.splitlines():
 url, filename = line.strip().split()
 getpml(url, filename)
answered Jan 3, 2013 at 1:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.