从python和字符串连接运行shell命令

从python和字符串连接运行shell命令,python,dropbox,Python,Dropbox,我正在尝试调整Snow loepard()的“投递到dropbox”服务。 我不想要公共URL,但要一个从goo.gl缩短的URL 因此,我使用以下shell命令: curl -s --data-urlencode "url=http://link.com" http://googl/action/shorten | grep "googl" | awk -F\" '{print $(NF-1)}' | awk 'BEGIN { FS = "=" } ; { print $2}' | pbcop

我正在尝试调整Snow loepard()的“投递到dropbox”服务。 我不想要公共URL,但要一个从goo.gl缩短的URL

因此,我使用以下shell命令:

curl -s --data-urlencode "url=http://link.com" http://googl/action/shorten | grep "googl" | awk -F\" '{print $(NF-1)}' | awk 'BEGIN { FS = "=" } ; { print $2}' | pbcopy
现在,python脚本执行以下操作,将其刚刚复制到公用文件夹中的所有文件的dropbox URL复制到剪贴板:

    pasteURLs = []

for file in copied_files: # for all elements in our list
    components = file.split(os.sep)    # seperate the path
    local_dir = os.sep.join(components[5:])    # cut off the beginning
    local_dir = urllib.quote(local_dir) # convert it to a URL (' ' -> '%20', etc.)
    #construct the URL
    finalURL = 'http://dl.dropbox.com/u/%s/%s' % ( dropbox_id, local_dir )
    pasteURLs.append(finalURL) # add the current URL to the string

copy_string = "\n".join(pasteURLs)
os.system( "echo '%s' | pbcopy" % (copy_string) ) # put the string into clipboard
我必须承认我对python一无所知,但从它的外观来看,我需要用以下内容更改最后两行:

shortURL = []
for thisURL in pasteURLs:
        shortURL = os.system( curl -s --data-urlencode "url=http://link.com" http://googl/action/shorten | grep "goo.gl" | awk -F\" '{print $(NF-1)}' | awk 'BEGIN { FS = "=" } ; { print $2}' | pbcopy )
    shortURLs.append(shortURL)

copy_string = "\n".join(shortURLs)
os.system( "echo '%s' | pbcopy" % (copy_string) ) # put the string into clipboard
但我的问题是,如何在命令中放置正确的URL?正如你看到的,上面写着
http://link.com
但它应该使用
此URL


有什么想法吗?提前谢谢

我认为您的os.system调用应该如下所示:

os.system("curl -s --data-urlencode \"url=%s\" http://goo.gl/action/shorten | grep \"goo.gl\" | awk -F\\\" '{print $(NF-1)}' | awk 'BEGIN { FS = \"=\" } ; { print $2}' | pbcopy " % thisURL)

我认为您的os.system调用应该如下所示:

os.system("curl -s --data-urlencode \"url=%s\" http://goo.gl/action/shorten | grep \"goo.gl\" | awk -F\\\" '{print $(NF-1)}' | awk 'BEGIN { FS = \"=\" } ; { print $2}' | pbcopy " % thisURL)

更新我为您编写了脚本,并使用了更简单的命令管道。并不是说在python中不使用curl就可以完成整个工作,而是在这里

import subprocess
thisURL = 'http://whatever.com'
pipeline = []
pipeline.append('curl -s -i --data-urlencode "url=%s" ' % thisURL +
    'http://goo.gl/action/shorten')
pipeline.append('grep Location:')
pipeline.append('cut -d = -f 2-')
#pipeline.append('pbcopy')
command = "|".join(pipeline)
link, ignore = subprocess.Popen(command, stdout=subprocess.PIPE,
        shell=True).communicate()
print link

更新我为您编写了脚本,并使用了更简单的命令管道。并不是说在python中不使用curl就可以完成整个工作,而是在这里

import subprocess
thisURL = 'http://whatever.com'
pipeline = []
pipeline.append('curl -s -i --data-urlencode "url=%s" ' % thisURL +
    'http://goo.gl/action/shorten')
pipeline.append('grep Location:')
pipeline.append('cut -d = -f 2-')
#pipeline.append('pbcopy')
command = "|".join(pipeline)
link, ignore = subprocess.Popen(command, stdout=subprocess.PIPE,
        shell=True).communicate()
print link

其他答案已经提供了这样的核心:在命令的周围使用引号,使用一个格式字符串插入值,并考虑使用子过程,以便实际获得命令的输出。


但是,如果您和我一样认为这有点太复杂了,那么可以看看如何在python中进行实际的缩短。如果您是python新手,这可能意味着您需要阅读您的应用程序才能理解它。(看起来也可能需要,但是如果你得到一个例外,它只会被使用…)

< P>其他的答案已经提供了这样的核心:在你的命令周围使用引号,使用一个格式字符串来插入这个值,并考虑使用子过程,以便实际获得命令的输出。
但是,如果您和我一样认为这有点太复杂了,那么可以看看如何在python中进行实际的缩短。如果您是python新手,这可能意味着您需要阅读您的应用程序才能理解它。(看起来您可能也需要,但再次强调,它似乎只有在出现异常时才使用…

基本上与@Daniel的相同,但三重引号避免了转义太多嵌入引号。哦,您需要使用subprocess.Popen。让我来解决这个问题。见我的实现答案:)基本上与@Daniel的相同,但三重引号避免了转义太多的嵌入引号。哦,你需要使用subprocess.Popen。让我来解决这个问题。有关实现,请参阅我的答案:)