Check if there is a file if there is a report of PYTHON Blibioteca > . os

Asked

Viewed 1,502 times

4

Check if the folder no longer exists, if it exists, you should print a message to the user indicating.

I got stuck not knowing how to check on something related to this :

os.listdir()
os.mkdir (input('Insira o nome:'))
if os.path.exists ('Users\Akros\Desktop\TESTE') == True:
  print ('Ja existe um arquivo com esse nome!')
else:
  print ("Criado com sucesso!")

The error that appears is :

File "C:\Users\Akros\Desktop\TESTE\OS.py", line 21, in sys_calls
os.mkdir (input('Insira o nome:'))
FileExistsError: [WinError 183] Não é possível criar um arquivo já existente: 'AKROS'

I’ve used.path.isdir / os.path.isfile / os.path.lexists .

This is an application for an exercise I’m doing at the federal institute, in case http://charles.garrocho.com/SO2016/documentos/trabalho_pratico_1_so.pdf in the case of this requirement it asks that if it is created or deleted a folder that already exists or does not exist to warn the user. But I can’t figure out which command or the logic of how to run it, it won’t go pro " print " error message appears.

  • What version of your python? in python2.x input requires a numeric type entry, to insert text must be the raw_input

  • At the institute they recommend us to use python 3.5 I never used python 2

1 answer

3


The problem is you’re trying to create the directory right on this line:

os.mkdir (input('Insira o nome:'))

And it launches the exception (this warning/error), because it should already exist. You should first see if it exists before creating, so:

import os.path

diretorio = input('Insira o nome:')
if os.path.isdir(diretorio): # vemos de este diretorio já existe
  print ('Ja existe um arquivo com esse nome!')
else:
  os.mkdir(diretorio) # aqui criamos o diretorio
  print ("Criado com sucesso!")

NOTE, that all the methods you used are for different things, in the end it is true that everyone checks the existence of something. But:

if os.path.isdir(path):  # Verifica se e diretorio
   ...

if os.path.isfile(path): # Verifica se e ficheiro
   ...

if s.path.exists(path): # verifica se existe, seja ficheiro (arquivo), ou diretorio
   ...

You need to adjust one of these to your case. Another thing, by the warning you give ('Already there is a file with that name!'), I see that you may be trying to create a file and not a directory, if that is the case change these two lines:

if os.path.isdir(diretorio):
...
os.mkdir(diretorio)

For

if os.path.isfile(diretorio):
...
ficheiro = open(diretorio, "w") # criamos/abrimos o ficheiro
#.... .... ... operacoes no ficheiro
ficheiro.close()
  • And that I searched the OS library to try to solve it but the information was a little hard to understand. And in the exercise I have to create a move rename and delete directory. In case any of the options is not available because it does not exist or already has to be reported. So I didn’t know which of the 3 options to use. And what is the logic to check if you already had any of the alternatives

  • I have no reputation to thank your reply, but she helped me finish thank you.

  • Nothing good has helped

  • I got the reputation and gave up a kk

  • If it’s directory it’s like I did on top of the full code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.