Tyler Morgan
Tyler Morgan

Reputation: 9

How to place a new file in a certian place on hard drive (python)

I am making a full python keylogger. It undergoes a simple processes. First store keystrokes inside a file on startup. Next, find the file and send the file across WiFi. Lastly, shutdown. For this to work I need to make a file for the keylogger to send the keystroke information to. I tried using:

open('myfile', 'w+')

This will create my file but how do I place my file into a certain place?

Extra Information:

Python 3.7x

Upvotes: -1

Views: 34

Answers (2)

micahlt
micahlt

Reputation: 424

I believe you're looking for a file path. The open() function in Python takes a file path and the read/write mode. In most programming languages and operating systems use dots and slashes to represent paths. Currently your script opens a file called "myfile" in the directory (folder) that your script is executing in. However, if you wanted to place that file one directory higher on the file tree, you could write the function as follows:

// Linux
open('../myfile', 'w+')
// Windows
open('..\myfile', 'w+')

Unfortunately, this method does require a knowledge of the system filetree.

Upvotes: 0

Sam
Sam

Reputation: 1415

  • You can add the path to the filename:

    open('/users/myname/myfile.txt', 'w+')
    open('C:\\Public\\myfile.txt', 'w+')
    
  • Or, you can change your current directory:

    import os
    os.chdir('/tmp/')
    open('myfile.txt', 'w+')
    

    Both should work! Happy Coding!

Upvotes: 1

Related Questions