使用 Python 打开现有文件时发生 IOError

发布于 2024-08-22 09:23:56 字数 1048 浏览 6 评论 0原文

运行以下代码:

import os
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y %H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    log = open ('/Users/admin/Documents/locatelog.txt','w')
    log.write(str("Asset: %s " % fqn))
    log.write(str("Checking in from IP#: %s" % ext_ip))
    smush = str(fqn +' @ ' + formatdate)
    os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/Documents/%s.txt' %  smush )

    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'r')
    s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

生成以下错误:

ftplib.error_perm: 550 /Users/admin/Documents/678538.local @ 02-24-2010 1301.txt: No such file or directory

我感觉这一行有问题:

s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

678538 是我正在测试的主机...使用 Mac OS X 10.5 和 Python 2.5.1

Running the following code:

import os
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y %H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    log = open ('/Users/admin/Documents/locatelog.txt','w')
    log.write(str("Asset: %s " % fqn))
    log.write(str("Checking in from IP#: %s" % ext_ip))
    smush = str(fqn +' @ ' + formatdate)
    os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/Documents/%s.txt' %  smush )

    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'r')
    s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

Generates the following error:

ftplib.error_perm: 550 /Users/admin/Documents/678538.local @ 02-24-2010 1301.txt: No such file or directory

I have a feeling something is amiss in this line :

s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

678538 is the host I am testing on...using Mac OS X 10.5 and Python 2.5.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小墨 2024-08-29 09:23:56

不应该是f = open('/Users/admin/Documents/%s.txt' % smush,'r')吗?注意Users前面的/

如果不放第一个/,脚本会认为文件的路径是相对于当前的目录(脚本运行的地方)

编辑

我不太熟悉Python(我希望),但不应该是:

s.storbinary('STOR /Users/admin/Documents/ %s.txt' % smush,f)

插入 smush 的值

在您的示例中,Python 会将您的字符串视为文字,并且您希望使用 %s Edit 2

:目录 /Users/admin/Documents/存在于你的服务器上吗?如果没有,我认为您必须在复制任何内容之前创建它们。 (因为错误消息是关于某些文件/文件夹丢失的)。

您可以先自己创建它们。运行你的脚本。如果文件复制成功,则您可以在脚本中添加目录的创建。

Shouldn't it bef = open('/Users/admin/Documents/%s.txt' % smush,'r') ? notice the / in front of Users

If you dont put the first /, the script will think the path to the file is relative to the current directory (where the script is run from)

Edit:

I m not too familiar with Python (I wish) but shouldnt it be:

s.storbinary('STOR /Users/admin/Documents/%s.txt' % smush,f) ?

In your example, Python will treat your string as literal and you want to interpolate the value of smush with %s

Edit 2:

Does the directory /Users/admin/Documents/ exist on your server? If not, I think you will have to create them before copying anything. (Since the error message is about some files/folders missing).

You can create them yourself first. Run your script. If the file is copied successfully, then you can add the creation of the directories from within your script.

苏别ゝ 2024-08-29 09:23:56

删除文件名中的所有空格。例如,在 smush = str(fqn +' @ ' + formatdate) 中,您在“@”之前和之后添加了一个空格。你的路径看起来像

/Users/admin/Documents/something @ something

,当你将它传递给 ftplib 时,它可能有问题。另一种方法是尝试加引号,例如

s.storbinary("STOR '/Users/admin/Documents/%s.txt'" % smush,f)

remove all spaces from file name .eg in smush = str(fqn +' @ ' + formatdate), you are putting a space in front of and after "@". you path looks like

/Users/admin/Documents/something @ something

and when you pass it to ftplib, it may have problem. another way is to try putting quotes, eg

s.storbinary("STOR '/Users/admin/Documents/%s.txt'" % smush,f)
尸血腥色 2024-08-29 09:23:56

编辑:

这个版本有效:问题是我正在写入缓冲区,而不是文件。

import os
import urllib2
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y-%H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    smush = str(fqn + formatdate)
    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'w')
    f.write(str("Asset: %s " % fqn))
    f.write('\n')
    f.write(str("Checking in from IP#: %s" % ext_ip))
    f.write('\n')
    f.write(str("On: %s" % formatdate))
    f.close
    f = open('/Users/admin/Documents/%s.txt' % smush,'rb')
    s.storbinary('STOR %s.txt' % smush , f)
    s.close
    f.close

Edit:

This version works: Problem was that I was writing to buffer, and not to file.

import os
import urllib2
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y-%H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    smush = str(fqn + formatdate)
    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'w')
    f.write(str("Asset: %s " % fqn))
    f.write('\n')
    f.write(str("Checking in from IP#: %s" % ext_ip))
    f.write('\n')
    f.write(str("On: %s" % formatdate))
    f.close
    f = open('/Users/admin/Documents/%s.txt' % smush,'rb')
    s.storbinary('STOR %s.txt' % smush , f)
    s.close
    f.close
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文