If the recording is given only the file name, where to find this file How to set the full path? code example:

fout = open("relativity", 'wt') fout.write(poem) fout.close() 

2 answers 2

You need to search for the file in the current program directory os.path.abspath(os.path.curdir)

 import os >>> os.path.curdir '.' >>> os.path.abspath(os.path.curdir) '/home/sa' 

You can set the full path directly in the file name. To separate directories, use os.path.sep , conveniently via os.path.join() . So your code will not depend on the operating system and the implementation of the language.

 >>> os.path.sep '/' >>> os.path.join('каталог','имя_файла') 'каталог/имя_файла' 

For Windows, it is permissible to use both \\ and / , but it is better to use os.path.join everywhere

    File search in the current directory of the interpreter. The full path is set as follows:

     fout = open("C:\\full\\path\\file.txt", 'wt')