【问题标题】:why is my code not writing to a new text file为什么我的代码没有写入新的文本文件
【发布时间】:2013-10-01 20:36:38
【问题描述】:

正在寻找关于为什么我的代码没有写入新文本文件的想法。没有错误可以给我提示。

def writeFile (filename, text):
file = open(greenBottle.txt, 'w')
file.write(text)
file.close()

    def main (text):

        big_nums = ['no','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten']
        text_one = (' green bottles \nHanging on the wall\n')
        small_nums = [' no',' one',' two',' three',' four',' five',' six',' seven',' eight',' nine',' ten']
        text_two = ('And if one green bottle \nShould accidentally fall\nThere\'ll be')
        text_three = (' green bottles \nHanging on the wall\n \n')
        result=[]
        text=new_string
        new_string=''

        for i in range(10, 0, -1):
            result.append(big_nums[i] + str( text_one))
            result.append(big_nums[i] + str( text_one))
            result.append(text_two + small_nums[i-1] + text_three)
            return result('')
            print(''.join(main(text)))

    if __name__ == '__main__':
        writeFile('greenBottle.txt',text)


    main(text)

【问题讨论】:

  • 什么操作系统?也许您没有对该文件夹的写入权限?
  • Win 7 我是管理员所以拥有所有权限
  • 如果没有错误,这不可能是您正在运行的代码。这有很多,例如text = new_string 在声明 new_string 之前,返回 result('') 当 result 是一个列表时(所以不可调用),当你调用 writeFile('greenBottle.txt',text) 时没有定义文本......向我们展示你的确切代码.
  • miles82 这就是我的方式

标签: python file python-3.x text


【解决方案1】:

就我而言,这些行是错误的,应该删除,而且你不能 return 来自 for 循环,而只能来自一个函数:

return result('')
print(''.join(main(text)))

这行重复了:

result.append(big_nums[i] + str( text_one))

在我重写你的代码之后:

def writeFile (filename, text):
    with open(filename,'w') as file:
        for line in text:
            file.write(line)

def main():
    result=[]
    big_nums = ['no','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten']
    text_one = (' green bottles \nHanging on the wall\n')
    small_nums = [' no',' one',' two',' three',' four',' five',' six',' seven',' eight',' nine',' ten']
    text_two = ('And if one green bottle \nShould accidentally fall\nThere\'ll be')
    text_three = (' green bottles \nHanging on the wall\n \n')
    new_string=''
    text = new_string

    for i in range(0,10):
        result.append(big_nums[i] + str( text_one))
        #result.append(big_nums[i] + str( text_one))
        result.append(text_two + small_nums[i-1] + text_three)
        #return result('')
        #print(''.join(main(text)))

    writeFile('greenBottle.txt',result)

main()

输出:

no green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be ten green bottles 
Hanging on the wall

One green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be no green bottles 
Hanging on the wall

Two green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be one green bottles 
Hanging on the wall

Three green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be two green bottles 
Hanging on the wall

Four green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be three green bottles 
Hanging on the wall

Five green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be four green bottles 
Hanging on the wall

Six green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be five green bottles 
Hanging on the wall

Seven green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be six green bottles 
Hanging on the wall

Eight green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be seven green bottles 
Hanging on the wall

Nine green bottles 
Hanging on the wall
And if one green bottle 
Should accidentally fall
There'll be eight green bottles 
Hanging on the wall

【讨论】:

  • @stackoverflow.com/users/2425215/k-dawg 谢谢好心的先生。不知道您不能从 for 循环返回,而只能从函数返回。但正如我所说,我只是一个初学者,所以谢谢你的知识
  • 他的for 循环在函数内部,所以他当然可以从中返回。
猜你喜欢
  • 1970-01-01
  • 2020-10-07
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多