Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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_Attributes_Character - Fatal编程技术网

奇怪的字符和属性的缺陷。python

奇怪的字符和属性的缺陷。python,python,attributes,character,Python,Attributes,Character,我已经创建了一个函数,并被它卡住了 功能的含义: 用户键入文件、编号和自己的姓名 程序将名称写入文件“number”的末尾 然后打印出文件的内容 有什么问题吗 当程序读取文件时,文件下面有奇怪的字符和很大的空间 像这样:਍圀漀爀氀搀眀椀搀攀㬀 ㈀  㐀 ⴀ 瀀爀攀猀攀渀琀ഀഀ (然后在Powershell中有一个巨大的空间容纳10-15行) 错误:“str”对象没有属性“close” def filemania(): print "Great! This way is called \

我已经创建了一个函数,并被它卡住了

功能的含义:

  • 用户键入文件、编号和自己的姓名

  • 程序将名称写入文件“number”的末尾

  • 然后打印出文件的内容

  • 有什么问题吗

  • 当程序读取文件时,文件下面有奇怪的字符和很大的空间
  • 像这样:਍圀漀爀氀搀眀椀搀攀㬀 ㈀  㐀 ⴀ 瀀爀攀猀攀渀琀ഀഀ (然后在Powershell中有一个巨大的空间容纳10-15行)

  • 错误:“str”对象没有属性“close”

    def filemania():
    
        print "Great! This way is called \"Filemania\""
    
        file_name = raw_input("Type in any text file> ")
        enter_1 = int(raw_input("Enter an integer> "))
        enter_2 = raw_input("Enter your name> ")
    
        print "Now your name will apear in the file %d times at the end" % enter_1
    
        open_file = open(file_name, 'a+')
        listok = []
    
        while len(listok) < enter_1:
            open_file.write(enter_2 + " ")
            listok.append(enter_2) 
    
        print "Contains of the file:"
        read_file = open_file.read()
        print read_file
        file_name.close()
    
    filemania()
    
    def filemania():
    打印“太好了!这种方式被称为“Filemania”
    文件名=原始输入(“键入任何文本文件>”)
    输入1=int(原始输入(“输入整数>”)
    输入\u 2=原始输入(“输入您的姓名>”)
    打印“现在您的姓名将在文件%d次结尾出现”%enter\u 1
    打开文件=打开(文件名“a+”)
    listok=[]
    而len(listok)<输入\u 1:
    打开\文件。写入(输入\ 2+“”)
    追加(输入_2)
    打印“文件的内容:”
    read_file=打开_file.read()
    打印读取文件
    文件名。关闭()
    filemania()
    
  • 我认为问题就在这里:

    打开文件=打开(文件名“a+”)


    有人知道如何解决这些问题吗?

    对于第二个错误,您正在尝试关闭原始输入字符串
    文件名。您的意思是关闭
    打开\u文件


    尝试并报告。

    首先设置
    文件名=原始输入(“键入任何文本文件>”)
    以便尝试用
    文件名关闭字符串。close()

    当您写入
    open\u file
    时,您将指针移动到文件的末尾,因为您正在追加
    read\u file=open\u file.read()
    不会按照您的想法执行

    您需要再次搜索文件的开头以打印内容,
    open\u file.seek(0)


    字符串如何具有close方法
    file\u name=raw\u input(“键入任何文本文件>”)
    您的文件对象是
    read\u file
    open\u file
    后者您永远不会关闭谢谢!您的代码变体可以工作。但是我是Python新手,我还不知道关于…as。是否可以使用argv或*args(将字符串更改为参数)以某种方式关闭文件?@NikitaShub,将哪个字符串更改为参数?我的意思是更改文件名。据我所知,这是一根弦。也许有一种方法可以将文件名作为参数?例如,从sys import argv,script,file_name=argvyes,如果要从命令行获取文件名运行它,请使用argv,结果相同。
    def filemania():
        print "Great! This way is called \"Filemania\""
        file_name = raw_input("Type in any text file> ")
        enter_1 = int(raw_input("Enter an integer> "))
        enter_2 = raw_input("Enter your name> ")
    
        print "Now your name will apear in the file %d times at the end" % enter_1
    
        # with automatically closes your files
        with open(file_name, 'a+') as open_file:
            listok = []
            # use range      
            for _ in range(enter_1):
                open_file.write(enter_2 + " ")
                listok.append(enter_2)
            print "Contains of the file:"
            # move pointer to start of the file again
            open_file.seek(0) 
            read_file = open_file.read()
            print read_file
    
    filemania()