How to place a new file in a certian place on hard drive (python) - python-3.x

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

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!

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.
If this answer helped you, I'd appreciate if you'd give it an upvote or mark it as correct!

Related

File updates between open() and first read() are not read

External Updates that happened to that file content during the time between open() and first read() are not returned in the read() content.
How can I get the latest file content from the read()?
I've tried flush() and seek(0) but didn't help.
https://repl.it/repls/RealGreedyTransfer#main.py
import time
def myfoo(handle):
print("myfoo started", flush=True)
time.sleep(50)
# External updates that happen during that time don't show up in read()
# foo.flush()
# foo.seek(0)
# can't close and re-open file handle
print(handle.read()) # <-- Not reading updates done after file open
# Upstream code base passing a file handle under an exclusive fcntl.lockf() lock
handle = open('temp.txt', 'r+')
myfoo(handle)
The issue is with the way files are written. Many text editors don’t just write to the file, they use a different method: they write to a temporary file, and then rename it to the original filename. Since renames are atomic in POSIX, in the event of a system crash during saving, the old version of the file will be available, and the new version might or might not be available in the temporary file.
For most purposes, this works as desired. The only exception is in this case, where you’re holding onto a file handle. Renames/moves/deletions do not affect the file handles, they are still open with the file they were opened with, even if that file is no longer accessible from the filesystem. You can experiment with this by opening a file, then removing it with rm, and then reading from the file — it will still show you the file contents from before you deleted it. You can also access the file in Linux inside /proc/XX/fd.
Your file handle won’t see changes, unless they are actually written (and flushed) to the same file (without the rename dance). If you’re working with something that writes by renaming, you would need to reopen the file to see the new contents.

How to open a text file from my desktop while using python 3.7.1 in Terminal

I saved a text file to my desktop named "test.txt" within the file I wrote only my name, David. Then, I opened terminal and opened python 3.7.1 and wrote the following code in attempt to see my name, David, populate:
open("/Users/David/Desktop/test.txt,"r")
However, I receive the following error message:
SyntaxError: EOL while scanning string literal
Does anyone know how I can avoid this error and have my name, David, read from the test.txt file on my desktop? Or am I going about this completely wrong?
As #Matt explained, you are missing quotes.
You can follow below approach to open file and read from it.
myfile = open("/Users/David/Desktop/test.txt","r") #returns file handle
myfile.read() # reading from the file
myfile.close() # closing the file handle, to release the resources.
For more information on how to do read/write operations on file
You are missing a quotation mark, after your file path. It should look like this:
open("/Users/David/Desktop/test.txt","r")
^ This quotation mark
This will open the file correctly, however you will still need to actually read from it.
You are missing the other quotations as the others have mentioned. Try using the with open statement, as it handles your resources for you, meaning you don't need to specify .close()
with open("/Users/David/Desktop/test.txt", "r") as file:
file.read()
you can use with which will close the file automatically as you come out of the block and put your Directory link
with open(r"Directory_link", "r") as file1:
FileContent = file1.read()
print(FileContent)

example of opening a new text file in a certain place on the computer in python

All the examples I find are in the current directory or are not examples at all. For the life of me, I can't figure out how to open a file in Python if it is not in the current directory.
Just use an absolute path.
f = open('C:/file.txt', 'w')
f.read()
If you actually wanted to change the working directory, use os.chdir

Matlab search path for all users on linux

How can I add a Matlab search path for all users on a Linux system?
I am managing a Linux computer that is shared by several people.
I want to place some Matlab *.m files at a path (for example, /usr/local/matlab/our_matlab_scripts/) which everyone has a read access permission.
I also want to add this path to the Matlab search paths of all users
so that they can start using the Matlab files immediately.
As a single user, I learned that I can add a search path by Matlab 'addpath' command or from the file menu of Matlab, for example.
However, so far I could not find a way to do it as an administrator for all the current and future users at once.
I would be grateful if you could kindly teach me.
If you look at this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560
you will find that there is a file called matlabrc.m that is executed at startup. It looks for the startup.m file that users can have. One could put the addpath commands in this file and it should work for all users.

How can you tell what files are currently open by any user?

I am trying to write a script or a piece of code to archive files, but I do not want to archive anything that is currently open. I need to find a way to determine what files in a directory are open. I want to use either Perl or a shell script, but can try use other languages if needed. It will be in a Linux environment and I do not have the option to use lsof. I have also had inconsistant results with fuser. Thanks for any help.
I am trying to take log files in a directory and move them to another directory. If the files are open however, I do not want to do anything with them.
You are approaching the problem incorrectly. You wish to keep files from being modified underneath you while you are reading, and cannot do that without operating system support. The best that you can hope for in a multi-user system is to keep your archive metadata consistent.
For example, if you are creating the archive directory, make sure that the number of bytes stored in the archive matches the directory. You can checksum the file contents before and after reading the filesystem and compare that with what you wrote to the archive and perhaps flag it as "inconsistent".
What are you trying to accomplish?
Added in response to comment:
Look at logrotate to steal ideas about how to handle this consistently just have it do the work for you. If you are concerned that rename of files will make processes that are currently writing them will break things, take a look at man 2 rename:
rename() renames a file, moving it
between directories if required. Any
other hard links to the file (as
created using link(2)) are unaffected.
Open file descriptors for oldpath are
also unaffected.
If newpath already exists it will be atomically replaced (subject
to a few conditions; see ERRORS
below), so that there is no point at
which another process attempting to
access newpath will find it missing.
Try ls -l /proc/*/fd/* as root.
msw has answered the question correctly but if you want to file the list of open processes, the lsof command will give it to you.

Resources