Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用os.utime进行更改后恢复文件修改次数_Python_Filemtime - Fatal编程技术网

在python中使用os.utime进行更改后恢复文件修改次数

在python中使用os.utime进行更改后恢复文件修改次数,python,filemtime,Python,Filemtime,我在Python2.7.1(在Mac OS X 10.7.5上运行)中遇到了一个关于OS.utime命令的问题 我正在尝试开发一个脚本,从符合特定条件的FTP下载文件,但如果该文件存在,并且我已经在本地目录中拥有该文件的副本,那么我想检查文件修改时间。如果它们不匹配,则我下载新副本。为了实现这一目标,我获取FTP文件修改时间,将其转换为时间戳,然后使用os.utime更改下载文件的访问和修改日期,以匹配FTP服务器的日期。 我的问题是,一旦我离开子程序,在那里我更改了访问和修改时间,它们就会恢复

我在Python2.7.1(在Mac OS X 10.7.5上运行)中遇到了一个关于OS.utime命令的问题

我正在尝试开发一个脚本,从符合特定条件的FTP下载文件,但如果该文件存在,并且我已经在本地目录中拥有该文件的副本,那么我想检查文件修改时间。如果它们不匹配,则我下载新副本。为了实现这一目标,我获取FTP文件修改时间,将其转换为时间戳,然后使用os.utime更改下载文件的访问和修改日期,以匹配FTP服务器的日期。 我的问题是,一旦我离开子程序,在那里我更改了访问和修改时间,它们就会恢复到原来的状态!我没有在后台运行任何东西,我还在linux服务器上测试了脚本,结果相同

如果您运行下面的代码两次,它将在调试注释中显示问题,因为FTP服务器中的文件没有更改,但时间戳与正确更改的本地文件不匹配。 提前感谢您在这个问题上的帮助

import ftplib
import os
from datetime import datetime

def DownloadAndSetTimestamp(local_file,fi,nt):
    lf=open(local_file,'wb')
    f.retrbinary("RETR " + fi, lf.write, 8*1024)
    lf.close
    print fi + " downloaded!"

    print "-> mtime before change : " + str(os.stat(local_file).st_mtime)   
    print "-> atime before change : " + str(os.stat(local_file).st_atime)   
    print "-> FTP value     : " + str(int(nt))
    #set the modification time the same as server for future comparison
    os.utime(local_file,( int(nt) , int(nt) ))
    print "-> mtime after change  : "+ str(os.stat(local_file).st_mtime)
    print "-> atime after change  : "+ str(os.stat(local_file).st_atime)

print "Connecting to ftp.ncbi.nih.gov..."   
f=ftplib.FTP('ftp.ncbi.nih.gov')
f.login()
f.cwd('/genomes/Bacteria/')
listing=[]
dirs=f.nlst();
print "Connected and Dir list retrieved."

target_bug="Streptococcus_pseudopneumoniae"
print "Searching for :"+ target_bug
ct=0;
Target_dir="test/"
for item in dirs:
    if item.find(target_bug)>-1:
        print item
        #create the dir
        if not os.path.isdir(os.path.join(Target_dir,item)):
            print "Dir not found. Creating it..."
            os.makedirs(os.path.join(Target_dir,item))
        #Get the gbk 
        #1) change the dir
        f.cwd(item)
        #2) get *.gbk files in dir
        files=f.nlst('*.gbk')
        for fi in files:
            print "----------------------------------------------"
            local_file = os.path.join(Target_dir,item,fi)
            if os.path.isfile(local_file):
                print "################"
                print "File " + local_file + " already exists."
                #get remote modification time           
                mt = f.sendcmd('MDTM '+ fi)
                #converting to timestamp
                nt = datetime.strptime(mt[4:], "%Y%m%d%H%M%S").strftime("%s")
                #print "mtime FTP :" + str(int(mt[4:]))
                #print "FTP M timestamp   : " + str(nt)
                #print "Local M timestamp : " + str(os.stat(local_file).st_mtime)
                #print "Local A timestamp : " + str(os.stat(local_file).st_atime)

                if int(nt)==int(os.stat(local_file).st_mtime):
                    print fi +" not modified. Download skipped"
                else:
                    print "New version of "+fi
                    ct+=1
                    DownloadAndSetTimestamp(local_file,fi,nt)
                    print "NV Local M timestamp : " + str(os.stat(local_file).st_mtime)
                    print "NV Local A timestamp : " + str(os.stat(local_file).st_atime)
                print "################"

            else:
                print "################"
                print "New file: "+fi
                ct+=1
                mt = f.sendcmd('MDTM '+ fi)
                #converting to timestamp
                nt = datetime.strptime(mt[4:], "%Y%m%d%H%M%S").strftime("%s")
                DownloadAndSetTimestamp(local_file,fi,nt)
                print "################"

        f.cwd('..')
f.quit()
print "# of "+target_bug+" new files found and downloaded: " + str(ct)

您缺少左前的括号。请关闭;它应该是
lf.close()


如果没有括号,则实际上无法关闭文件。相反,在您调用
os.utime
之后,垃圾收集器会在稍晚一点关闭该文件。由于关闭文件会刷新未完成的IO缓冲区内容,因此修改时间将作为副作用进行更新,从而使以前设置的值失效。

您缺少了
lf.close中的括号。
;它应该是
lf.close()


如果没有括号,则实际上无法关闭文件。相反,在您调用
os.utime
之后,垃圾收集器会在稍晚一点关闭该文件。由于关闭文件会刷新未完成的IO缓冲区内容,因此修改时间将作为一个副作用进行更新,将刷新您以前设置的值。

谢谢!这确实是问题所在。我对python很陌生,这些没有消息/警告的情况仍然让我感到困惑!我希望不关闭括号会导致异常。@JAC它不会导致异常,因为
lf.close
是一个有效的表达式-它的计算结果是绑定到特定实例
lf
close
方法。例如,您可以说,
fn=lf.close
,然后将
fn
发送给将其称为
fn()
的代码,并产生与调用
lf.close()
相同的效果。谷歌“绑定方法”了解更多细节。在您的情况下,
lf.close本身的行不是很有用,但是
2+2
也不是,它也不会导致异常。谢谢!这确实是问题所在。我对python很陌生,这些没有消息/警告的情况仍然让我感到困惑!我希望不关闭括号会导致异常。@JAC它不会导致异常,因为
lf.close
是一个有效的表达式-它的计算结果是绑定到特定实例
lf
close
方法。例如,您可以说,
fn=lf.close
,然后将
fn
发送给将其称为
fn()
的代码,并产生与调用
lf.close()
相同的效果。谷歌“绑定方法”了解更多细节。在您的情况下,
lf.close本身的行不是很有用,但是
2+2
,它也不会导致异常。