Traceback (most recent call last):
File "c:\Users\*\Documents\GitHub\_Testes\novo_teste.py", line 1, in <module>
teste = open('teste.txt', 'w')
^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'teste.txt'
I don't understand why it doesn't create the file and instead returns this error
I already tried 'w', 'w+', 'x', 'x+'. It simply only accepts if I use the absolute path, shouldn't it accept using the script directory as a base?
I tried using 'os.getcwd()':
import os
print(os.getcwd()) # Show the actual directory
arquivo = open(os.getcwd()+"\\teste.txt", "w")
arquivo.write('teste')
arquivo.close()
result:
PS C:\Users\*\Documents\GitHub\_Testes> & C:/Users/*/AppData/Local/Programs/Python/Python311/python.exe c:/_Projetos/_Testes/testes.py
C:\Users\*\Documents\GitHub\_Testes
Traceback (most recent call last):
File "c:\_Projetos\_Testes\testes.py", line 3, in <module>
arquivo = open(os.getcwd()+"\\teste.txt", "w")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\*\\Documents\\GitHub\\_Testes\\teste.txt'