Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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-在函数中更改文件对象_Python_File_Function_Object - Fatal编程技术网

Python-在函数中更改文件对象

Python-在函数中更改文件对象,python,file,function,object,Python,File,Function,Object,抱歉-我的问题是如何从其他函数更改函数中的文件对象 我在我的第一个python脚本中尝试解决这个错误已经太久了,Google博士和论坛对我帮助不大,但我希望你能 我有一个生成大量数据的循环函数,我想将其输出到一个文本文件,并在第三个循环后创建一个新的文本文件。 我定义了两个函数,一个用于创建数据哈希,另一个用于创建新文件 新文件正在按预期创建(aaa.txt、baa.txt…等),但“hashit”函数只会写入第一个文件(aaa.txt),即使其他文件正在创建 我尝试了fo.close()fo.

抱歉-我的问题是如何从其他函数更改函数中的文件对象

我在我的第一个python脚本中尝试解决这个错误已经太久了,Google博士和论坛对我帮助不大,但我希望你能

我有一个生成大量数据的循环函数,我想将其输出到一个文本文件,并在第三个循环后创建一个新的文本文件。 我定义了两个函数,一个用于创建数据哈希,另一个用于创建新文件

新文件正在按预期创建(aaa.txt、baa.txt…等),但“hashit”函数只会写入第一个文件(aaa.txt),即使其他文件正在创建

我尝试了fo.close()fo.flush(),以及在函数中引用fo,但似乎无法使其工作。我还将fo.write从函数移到了主体

我已经包括了我用来解决这个问题的代码的简化版本,真正的版本有多个增加字符串长度的循环

提前谢谢

import smbpasswd, hashlib
base = '''abcdefghijklmnopqrstuvwxyz '''
# base length 95
print(base)

baselen = len(base)
name = 'aaa.txt'
fo = open(name, "w")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
pw01 = 0
pw02 = 0
pw03 = 0

def hashit(passwd):
    #2
    # Need to install module
    # sudo apt-get install python-smbpasswd
    hex_dig_lm = smbpasswd.lmhash(passwd)               
    hex_dig_ntlm = smbpasswd.nthash(passwd)
    #print '%s:%s' % smbpasswd.hash(passwd)
    hash_md5 = hashlib.md5(passwd)
    hex_dig_md5 = hash_md5.hexdigest()

    print(passwd)
    print(hex_dig_lm)
    print(hex_dig_ntlm)
    print(hex_dig_md5)
    hashstring = passwd +","+ hex_dig_lm +","+ hex_dig_md5 + '\n'
    fo.write(hashstring);

def newfile(name):
    fo.flush()
    fo = open(name, "a")
    print("-------newfile------")
    print "Name of the file: ", fo.name
    print "Closed or not : ", fo.closed
    print('NewFile : ' + name)
    raw_input("\n\nPress the enter key to exit.")

# add 3rd digit
while (pw03 < baselen):
    pwc03 = base[pw03]
    name = pwc03 + 'aa.txt'
    fo.close
    newfile(name);
    pw03 += 1 
    while (pw02 < baselen):
        pwc02 = base[pw02]
        pw02 += 1
        while (pw01 < baselen):
            pwc01 = base[pw01]
            pw01 += 1
            passwd = pwc03 + pwc02 + pwc01
            hashit(passwd);
        else:
            pw01 = 0   
    else:
           pw02 = 0
else:
    pw03 = 0
导入smbpasswd、hashlib
基数=''abcdefghijklmnopqrstuvxyz''
#基长95
打印(基本)
基本长度=基本长度(基本)
名称='aaa.txt'
fo=打开(名称“w”)
打印“文件名:”,fo.Name
打印“是否关闭:”,fo.Closed
打印“打开模式:”,fo.mode
打印“软空间标志:”,fo.Softspace
pw01=0
pw02=0
pw03=0
def hashit(passwd):
#2
#需要安装模块吗
#sudo apt get安装python smbpasswd
hex_dig_lm=smbpasswd.lmhash(passwd)
hex\u dig\u ntlm=smbpasswd.nthash(passwd)
#打印“%s:%s”%smbpasswd.hash(passwd)
hash_md5=hashlib.md5(passwd)
hex_dig_md5=hash_md5.hexdigest()
打印(passwd)
打印(十六进制数字)
打印(十六进制数字)
打印(十六进制数字md5)
hashstring=passwd+“,“+hex\u dig\u lm+”,“+hex\u dig\u md5+”\n
fo.write(哈希字符串);
def新文件(名称):
fo.flush()
fo=打开(名称“a”)
打印(“----新文件----”)
打印“文件名:”,fo.Name
打印“是否关闭:”,fo.Closed
打印('NewFile:'+名称)
原始输入(“\n\n按enter键退出”)
#添加第三位数字
而(pw03
在newfile()函数中,首先添加以下行:

global fo

你的问题是什么?你的代码中有太多的
else
;实际问题是什么?感谢您的输入,我使用else将计数器重置为0,“#添加第三位”下的部分被重新定位为第四位和第五位等。问题是fo.写入(哈希字符串);hashit函数下的语句仅写入aaa.txt,即使我将fo更改为baa.txt、caa.txt等。新文件正确地生成了baa.txt,但hashit仍然使用aaa.txt非常感谢!这工作得很好,我真的很感激。我标记为已回答,但它不会让我+1你,因为我是一个新用户,但这里有一个非正式的+1无论如何。。。再次感谢