example of opening a new text file in a certain place on the computer in python - python-3.x

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

Related

.txt file is opening but prints nothing

I'm trying to open a text file and print it as a string. I've made sure there is text in the .txt file but when I run the code it just prints an empty space, I don't know what to do at this point since I couldn't find anything that could help me with my problem.
with open('test.txt', 'r') as file:
data = file.read().rstrip()
print(data)
When things aren't opening check the following:
You wrote exactly the same in your code as the one you saved. "file.txt" is not the same as "File.txt" for Python (same goes for accents and special characters).
The file you are trying to read is in the same directory. If your code is at users/bla/documents/another_folder and you just pass the name of the file to your code, then the file must be at users/bla/documents/another_folder too. If not, be shure to add it into the string path as "path/to/your/file/file.txt"
Make sure that the extension .txt is the same as your file.
If you checked that but everything seems correct, try:
with open(path_to_file) as f:
contents = f.readlines()
And see if "contents" has something.
I think it is better if you use open("file.txt","r") function to do it. So your code will be like this:
file=open("test.txt","r")
data=file.read().strip()
print(data)

Move files to their respective folder in linux based on the date of the folder and the date of the file

I'm fairly new to bash scripting and linux, and I have a folders with just dates such as
2012-11-20
2012-11-21
2012-11-22
2012-11-23
and I have files with the name data_11202012_randomnumbers_csv.
I would like to create a script that can move every single csv file to it's correct folder by matching the date on the file to the folder.
I've been just typing mv file path but i have 100s of files and I'm wondering if theres an easier way.
Any help would be appreciated.
The following should do it for you. I will explain with comments
for file in your_folder/*; do
# 1. Extract the numbers from the file name
dir="${file#data_}" # remove data_ prefix
dir="${dir%%_*}" # remove everything after first _
# 2. Rearrange the numbers into the desired format
dir="${dir:2:4}-${dir:0:2}-${dir:6:2}"
# 3. Move the file into the directory
mv file dir
done
Here you have a very useful bash cheatsheet where you can learn more about it. It illustrates all the variable expansions I've made in my snippet and more.

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
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!

How do I tell my program to search for a file that may have a slightly varied filepath?

So I have a program right now that reads text files from a folder. They are in a separate folder than the one my program is in, and both of them are in a dropbox folder together. Right now if I want to read one of these .txt files I'll do
file='/Users/JohnDoe/application/folder/file.txt'
text=open(file,'r').read()
print(text)
and it works just fine on my pc. The problem is that if I open my dropbox on another computer and run this program, it won't work because the filepath will probably be slightly different. Is there a way to work around this? Maybe a module that searches for files using something other than filepath, or something like that.
I'm brand new to python and to programming in general so any suggestions are appreciated. Thanks.
If you know the specific name of the file, but you're unsure where it might be located on another machine, you can use os.walk like this previous answer suggests. Or you can read up on using regex (regular expressions) in Python like this previous answer suggests.

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)

Resources