[英]Python 2.7: writing output to a file located on a shared directory using url
I am trying to test a simple way to write a message from a python script running locally to a file (that may not exist at the time of the script run) that can be hosted in a shared directory (ie a dropbox link) and is whose path is specified by a url. 我正在尝试测试一种简单的方法,将消息从本地运行的python脚本写入文件(该脚本在运行时可能不存在),该文件可以托管在共享目录(即保管箱链接)中,并且其路径由网址指定。
1) Dropbox is just chosen as an example since it is well known and easy. 1)由于Dropbox是众所周知的且容易实现,因此仅以Dropbox为例。 I have been able to retrieve contents from a file that already exists in a shared directory via a dropbox link if I change the last dl = 0 to dl = 1.
如果我将最后一个dl = 0更改为dl = 1,则可以通过Dropbox链接从共享目录中已存在的文件中检索内容。
Example: (Not using a real link): I have a testfile.txt with a simple message written: 'this is my test file' 示例:(不使用真实链接):我有一个testfile.txt,上面写着一条简单的消息:“这是我的测试文件”
import urllib2
#this link would point directly to the shared file location
the_link = 'https://www.dropbox.com/.....blahblah....?dl=1'
response = urllib2.urlopen(the_link)
print response.read()
#print's the message written in the text file
>>: 'this is my test file'
2) What I would like to do is go the other direction - ie run my script that prints a message, and then saves this to a url based shared directory (dropbox?), creating the file if necessary. 2)我想做的是朝另一个方向走-即运行我的脚本以打印一条消息,然后将其保存到基于url的共享目录(投递箱?),并在必要时创建文件。
I would like to avoid using the dropbox API (since I may not end up using dropbox at all in a final solution) and I may end up using a served directory or some other url based resource. 我想避免使用Dropbox API(因为在最终解决方案中最终可能根本不会使用dropbox),并且最终可能会使用服务目录或其他基于url的资源。 The objective is to have the file read/writeable and hosted at a url that can be accessed from anywhere.
目的是使文件可读/写,并托管在可以从任何地方访问的URL中。
Where I am stuck/things I have considered: 我被困住的地方/我考虑过的事情:
1) using os? 1)使用操作系统? os.chdir() and getting directory info using the url.
os.chdir()并使用url获取目录信息。 Then writing to file using open(filename, w), etc?
然后使用open(filename,w)等写入文件?
2) using urllib2 to make a file? 2)使用urllib2来制作文件? And then write to it?
然后写呢?
Any advice is greatly appreciated. 任何意见是极大的赞赏。 Thanks in advance.
提前致谢。
Your best solution is to use the API distributed by Dropbox itself. 最好的解决方案是使用Dropbox本身分发的API。 Take a look here .
在这里看看。
Also, here explains how to use the Dropbox API to save a file to your personal dropbox. 另外, 这里说明了如何使用Dropbox API将文件保存到您的个人保管箱。
If you are working with another url based shared directory , look for existing APIs. 如果您正在使用另一个基于url的共享目录 ,请查找现有的API。 If not, you will have to program it yourself.
如果没有,则必须自己编程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.