AttributeError:'nt.stat_result'对象没有属性'S_IWRITE' [英] AttributeError: 'nt.stat_result' object has no attribute 'S_IWRITE'

查看:448
本文介绍了AttributeError:'nt.stat_result'对象没有属性'S_IWRITE'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除文件。我检查日期并删除它是否比我想要的旧。我注意到一个.zip文件没有删除。它是只读的,所以在一些测试代码中,我使用os.chmod(path,stat.S_IWRITE)然后os.remove(路径),它工作。我把这段代码放到我的主代码中并得到了错误。我在两者中导入了os和stat模块。



下面是可以使用的测试代码,但是当我把它放到更大的代码中时,我得到了错误和完整的代码这只是一个def:



AttributeError:'nt.stat_result'对象没有属性'S_IWRITE'



<我已经检查过dpath和dayscount正在传递路径和天数。

  import os,stat 

def del_file(dpath,dayscount):
if dayscount > 10:
如果dpath [-4:] ==。zip:
os.chmod(dpath,stat.S_IWRITE)
os.remove(dpath)
else:
os.remove(dpath)
else:
printFile is Good

dpath =C:\ Workspace \ Test_Data.zip
dayscount = 15
del_file(dpath,dayscount)

在这里阅读后我发现了链接:此页面上的代码,其中显示了导入模块的一些示例。答案说要在函数中导入模块。我在我的主代码上尝试了这个并且它有效。



主代码在代码顶部导入os和stat,但是这个函数似乎没有看到它。而且我不确定为什么。当我在模块中导入它时,它会看到它。因此,当放入主代码时,我必须将导入添加到模块中,请参见下文。

  import os,stat 

def test_date():
此函数的代码

def get_month():
此函数的代码

def del_file (dpath,dayscount):
import os,stat
print dpath
print dayscount
if dayscount> 10:
如果dpath [-4:] ==。zip:
os.chmod(dpath,stat.S_IWRITE)
#os.remove(dpath)
else :
os.remove(dpath)
else:
printFile is Good

dpath =C:\ Workspace \ Test_Data.zip
dayscount = 13
del_file(dpath,dayscount)


解决方案

您在代码中的某处设置了一个名为 stat 的变量(到 os.stat 致电)。这个变量掩盖了统计模块。



搜索 stat = 并将变量重命名为另一个名称,如 stat_result


I am trying to delete a files. I check the date and delete if it is older than I want. I noticed that one .zip file was not deleting. It was read only, so in a bit of test code, I used the os.chmod(path, stat.S_IWRITE) and then os.remove(path) and it worked. I put this code into my main code and got the error. I import the os and stat module in both.

Below is the test code that works, but when I put this into the bigger code I got the error and the full code this is just a def as well:

AttributeError: 'nt.stat_result' object has no attribute 'S_IWRITE'

I had checked that the dpath and dayscount are passing the path and number of days.

import os, stat

def del_file(dpath, dayscount):
    if dayscount > 10:
        if dpath[-4:]== ".zip":
            os.chmod(dpath,stat.S_IWRITE)
            os.remove(dpath)
        else:
            os.remove(dpath)
    else:
        print "File is Good"

dpath = "C:\Workspace\Test_Data.zip"
dayscount = 15
del_file(dpath, dayscount)

After reading here I found a link:Code on this page that showed some examples of importing modules. The answer said to import the module in the function. I tried this on my main code and it worked.

The main code imports os and stat at the top of the code, but this function doesn't seem to see it. And I'm not sure why. When I import it in the module it then sees it. So when put in the main code I had to add the import into the module, see below.

import os, stat

def test_date():
Code for this function

def get_month():
Code for this function

def del_file(dpath, dayscount):
    import os, stat
    print dpath
    print dayscount
    if dayscount > 10:
        if dpath[-4:]== ".zip":
            os.chmod(dpath,stat.S_IWRITE)
            #os.remove(dpath)
        else:
            os.remove(dpath)
    else:
        print "File is Good"

dpath = "C:\Workspace\Test_Data.zip"
dayscount = 13
del_file(dpath, dayscount)

解决方案

You are setting a variable with the name stat somewhere in your code (to the result of an os.stat call). This variable overshadows the stats module.

Search for stat = and rename the variable to another name, like stat_result.

这篇关于AttributeError:'nt.stat_result'对象没有属性'S_IWRITE'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆