Create file without knowing directory? Python

0

It is known that it is possible to create a new file through Python through file_create = open(r'C:\Diretório\Diretório...\FileName', 'w') . However, it is necessary to indicate where this file will be created on the computer.

If we wanted to create a file on an unknown computer, we would download the Python program, how would that be possible? Considering that to originate content that is in a specific location and / or explain on the computer (Desktop or some folder), we need the three elements that precede the 'Desktop' in C: \ Users \ Windows 7 \ Desktop: o 'C ', the' Users', and the username used.

Is there any general command? Or ask the user where they want the data to go?

    
asked by anonymous 09.04.2018 / 19:41

1 answer

1

Hello,

You can get the computer's user name from the system using getpass. See:

import getpass
usuario = getpass.getuser()

Then you can try something like this:

file_create = open("C:\Users\" + usuario + "\Desktop\filename", "w")
    
09.04.2018 / 19:55