打开文本文件时出现错误提示

时间:2019-03-17 14:21:35

标签: python-3.x

我想打开一个文件以编辑其中的内容,但出现此错误

Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/LittleSmashBros/Editeur de niveau.py", line 95, in <module>
    fichier = open("niveau1.txt",)
TypeError: Required argument 'flags' (pos 2) not found

这不是我第一次编码,但是我对此很陌生。我的文本文件在正确的文件中,之前可以使用。当我添加函数fichier_temp时,它停止工作。如果您能找到问题所在,并说明如何解决。这是我的代码,对不起,变量名,我是法国人。

from tkinter import *
from os import *

fenetre = Tk()
fenetre.title("LittleSmashBros")
fenetre.geometry("1110x720")
bgImage = PhotoImage(file="Greenbg.png")
fond = Canvas(fenetre,width=1110,height=720)
fond.place(x=-2,y=-2)
fond.create_image(0,0,image=bgImage,anchor="nw")
fond.create_rectangle(1080,0,1111,720,fill="black")

#Stocke les images dans des variables
platGauche = PhotoImage(file="platGauche.png")
platMilieu = PhotoImage(file="platMilieu.png")
platDroite = PhotoImage(file="platDroite.png")

#Bouton qui définissent le bloc choisi
typeBloc = "V"
couleurB = "black"
def changeBloc(bloc): typeBloc = bloc
def boutonEnfonce(bouton) :
    bloc1.config(bg=couleurB)
    bloc2.config(bg=couleurB)
    bloc3.config(bg=couleurB)
    bouton.config(bg="white")
bloc1 = Button(fenetre,image=platGauche,height=21,width=21,relief=FLAT,bg=couleurB,command=lambda : [changeBloc("G"),boutonEnfonce(bloc1)])
bloc2 = Button(fenetre,image=platMilieu,height=21,width=21,relief=FLAT,bg=couleurB,command=lambda : [changeBloc("M"),boutonEnfonce(bloc2)])
bloc3 = Button(fenetre,image=platDroite,height=21,width=21,relief=FLAT,bg=couleurB,command=lambda : [changeBloc("D"),boutonEnfonce(bloc3)])
bloc1.place(x=1082,y=10)
bloc2.place(x=1082,y=41)
bloc3.place(x=1082,y=72)

#Prend les coordonnées de la souris et donne sa position sur la grille
def cordsouris(cord):
    global x0,y0
    x0 = cord.x
    y0 = cord.y
    x1 = 0
    x2 = 22
    y1 = 0
    y2 = 22
    testx = False
    testy = False
    colonne = 0
    ligne = 0
    while testx == False:
        if x1 < x0 < x2:
            testx = True
            colonne = colonne + 1
        else:
            colonne = colonne + 1
            x1 = x1+21
            x2 = x2+21
    print(colonne)

    while testy == False:
        if y1 < y0 < y2:
            testy = True
            ligne = ligne + 1
        else:
            ligne = ligne + 1
            y1 = y1+21
            y2 = y2+21
    print(ligne)
    return (colonne,ligne)

#Créé la ligne a remplacer
def remplace_str_index(ligne,place,remplacement):
    texte = fichier.readline(ligne)
    return '%s%s%s'%(texte[:place],remplacement,texte[place+1:])

#Copie l'ancien fichier dans un nouveau avec la modification
def fichier_temp():
    fichier = open("niveau1.txt")
    colonne,ligne = cordsouris()
    with open("temp.txt","w")as temp:
        nb = 0
        for rang in fichier:
            nb = nb+1
            if nb==ligne: text = remplace_str_index(ligne,colonne-1,typeBloc)
            else : text = rang
            temp.write(text)
            print(rang)
            print(text)
    fichier.close()
    os.remove("niveau1.txt")
    os.rename("temp.txt","niveau1.txt")


#Détecte le click et effectue le changement de bloc
fond.bind("<Button 1>",fichier_temp)

#Place l'image en fonction du fichier texte
fichier = open("niveau1.txt",)
x=0
y=0
for rang in fichier:
    for caractere in rang:
        if caractere=="S":
            fond.delete(fond.find_closest(x,y))
        if caractere=="G":
            fond.create_image(x,y,image=platGauche,anchor="nw")
        if caractere=="M":
            fond.create_image(x,y,image=platMilieu,anchor="nw")
        if caractere=="D":
            fond.create_image(x,y,image=platDroite,anchor="nw")
        x = x+21
    x = 0
    y = y+21
fichier.close()

fenetre.mainloop()

3 个答案:

答案 0 :(得分:2)

这就是为什么您永远不要使用<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"> <environmentVariables /> </aspNetCore> </system.webServer> </configuration> 的原因。

from X import *from os import *覆盖了Python中的内置open(与内置os.open不同,它没有模式)。

要么删除星号导入,要么像使用open的其他位置一样提供标记。

答案 1 :(得分:1)

您需要提供文件打开模式

open("niveau1.txt", "r")


答案 2 :(得分:1)

这是问题所在:

from os import *

它还会导入os.open,现在它会覆盖建筑物open

如果仅从os导入所需的零件,它将正常工作。

相关问题