Fórum Erro ao excluir arquivo em Python3 #603638
14/07/2019
0
from tkinter import *
from tkinter import ttk
from os import mkdir, chdir, path, remove
from tkinter import messagebox
import shutil
inicio = open('config/config.txt', 'r')
ini = int(inicio.readline())
ini_j = int(inicio.readlines()[0])
print(ini, ini_j)
inicio.close()
permitir = 0
def atualizar_variavel():
if not(path.exists('config')):
chdir('../')
inicio = open('config/config.txt', 'r')
ini = int(inicio.readline())
ini_j = int(inicio.readlines()[0])
print(ini, ini_j)
inicio.close()
def menu():
def apagar_arquivos_do_projeto():
pergunta = messagebox.askquestion('Excluir', 'Você deseja apagar o projeto')
if pergunta == 'yes':
chdir('../')
info = open('info.txt', 'r')
nome = info.readline()
info.close()
shutil.rmtree(nome)
remove('info.txt')
conf = open('config/config.txt', 'w')
conf.write('0\\n')
conf.write('0\\n')
conf.close()
janela.destroy()
exit()
def c_j():
if ini_j == 0:
chdir('../')
inicio = open('config/config.txt', 'w')
inicio.write('1\\n')
inicio.write('1')
inicio.close()
info = open('info.txt', 'r')
nome = info.readline()
info.close()
def e_code():
messagebox.showinfo('alerta', 'O programa será fechado para salvar as informações')
chdir(nome) # Atenção
arq = open('jogo.py', 'a')
arq.write(f'\\nscreen = pygame.display.set_mode(({tamanho1.get()}, {tamanho2.get()}))\\n')
arq.write('\\nwhile True:\\n')
arq.write(' for event in pygame.event.get():\\n')
arq.write(' if event.type == QUIT:\\n')
arq.write(' pygame.quit()\\n')
arq.write(' exit()')
arq.close()
c.destroy()
janela.destroy()
exit()
c = Toplevel()
c.title('Criar janela principal')
info = Label(c, text='Digite o tamanho da janela')
info.pack()
tamanho1 = Entry(c)
tamanho1.pack()
x = Label(c, text='X')
x.pack()
tamanho2 = Entry(c)
tamanho2.pack()
ok = Button(c, width=16, text='Comfirmar', relief='groove', command=e_code)
ok.pack(padx=20, pady=60)
c.transient(janela)
c.focus_force()
c.grab_set()
c.geometry('300x300')
else:
messagebox.showinfo('Alerta', 'Você não pode criar mais de uma janela')
janela = Tk()
janela.title('Menu Maker')
#atualizar_variavel()
#Criando uma aba 1
sub_j = ttk.Notebook(janela)
aba1 = ttk.Frame(sub_j)
sub_j.add(aba1, text='Aba 1')
sub_j.pack(expand=1, fill='both')
# Area da aba 1
criar_janela = Button(aba1, width=16, text='criar janela', relief='groove', command=c_j)
criar_janela.place(x=0, y=0)
apagar_projeto = Button(aba1, width=16, text='apagar projeto', relief='groove', command=apagar_arquivos_do_projeto)
apagar_projeto.place(x=125, y=0)
janela.geometry('700x600')
janela.mainloop()
def criar_projeto():
global inicio
try:
nome = str(nome_do_projeto.get())
if not(path.exists(nome)) and ini != 1:
mkdir(nome)
chdir(nome)
arq = open('jogo.py', 'a')
if ini == 0:
arq.write('import pygame\\n')
arq.write('from pygame.locals import *\\n')
arq.write('\\npygame.init()\\n')
arq.close()
chdir('../')
inicio = open('config/config.txt', 'w')
inicio.write('1\\n')
inicio.write('0')
inicio.close()
info = open('info.txt', 'a')
info.write(nome)
info.close()
chdir(nome)
#arq.close()
janela.destroy()
menu()
except:
print('erro')
instrucao['text'] = 'erro'
janela = Tk()
janela.title('maker')
titulo_ini = Label(janela, pady=20, text='MAKER', font='impact 20 bold')
titulo_ini.pack()
instrucao = Label(janela, text='Nome do projeto')
instrucao.pack()
nome_do_projeto = Entry(janela)
nome_do_projeto.pack()
criar = Button(janela, width=15, text='criar', relief='groove', command=criar_projeto)
criar.pack(padx=20, pady=60)
if ini == 1:
criar['text'] = 'Abrir'
janela.geometry('300x300')
janela.mainloop()
quando executo o código pela segunda vez e clico em apagar projeto o Python me retorna esse erro:
PermissionError: [WinError 32] O arquivo já está sendo usado por outro processo: 'jogo\\\\jogo.py'
Walefy Gonçalves
Curtir tópico
+ 0
Responder
Post mais votado
14/07/2019
O comando que fecha o arquivo jogo.py está comentado (#arq.close()). Ele abre o arquivo e nunca mais fecha. Então quando você encerra o programa, o arquivo fica aberto na memória.
Vinnicyus Leite
Responder
Gostei + 1
Mais Posts
15/07/2019
Walefy Gonçalves
O comando que fecha o arquivo jogo.py está comentado (#arq.close()). Ele abre o arquivo e nunca mais fecha. Então quando você encerra o programa, o arquivo fica aberto na memória.
Obrigado!
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)