How to write file in a different directory in python? - python

I am working on Linux with python 2.7.x and I am running some programs python through terminal. I want the certain output should be written in a file located at different directory than my working directory. So I wrote this piece of code. However, what is happening is file All.txt is being created in current directory instead of the desired directory. Can someone help me where I went wrong?
ResultDir = '/pr/p1/ap11/'
os.system('cd ' + ResultDir)
Outputname1 = 'All.txt'
Output1 = open(Outputname1, 'a')
Output1.write('hello' +'\n')
Output1.close()

Changing the current directory with os.system will not affect the Python process that’s running. Just open the file with its full path directly:
with open('/pr/p1/ap11/All.txt', 'a') as output:
output.write('hello\n')

Related

Python - File Path not found if script run from another directory

I'm trying to run a script that works without issue when I run using in console, but causes issue if I try to run it from another directory (via IPython %run <script.py>)
The issue comes from this line, where it references a folder called "Pickles".
with open('Pickles/'+name+'_'+date.strftime('%y-%b-%d'),'rb') as f:
obj = pickle.load(f)
In Console:
python script.py <---works!
In running IPython (Jupyter) in another folder, it causes a FileNotFound exception.
How can I make any path references within my scripts more robust, without putting the whole extended path?
Thanks in advance!
Since running in the console the way you show works, the Pickles directory must be in the same directory as the script. You can make use of this fact so that you don't have to hard code the location of the Pickles directory, but also don't have to worry about setting the "current working directory" to be the directory containing Pickles, which is what your current code requires you to do.
Here's how to make your code work no matter where you run it from:
with open(os.path.join(os.path.dirname(__file__), 'Pickles', name + '_' + date.strftime('%y-%b-%d')), 'rb') as f:
obj = pickle.load(f)
os.path.dirname(__file__) provides the path to the directory containing the script that is currently running.
Generally speaking, it's a good practice to always fully specify the locations of things you interact with in the filesystem. A common way to do this as shown here.
UPDATE: I updated my answer to be more correct by not assuming a specific path separator character. I had chosen to use '/' only because the original code in the question already did this. It is also the case that the code given in the original question, and the code I gave originally, will work fine on Windows. The open() function will accept either type of path separator and will do the right thing on Windows.
You have to use absolute paths. Also to be cross platform use join:
First get the path of your script using the variable __file__
Get the directory of this file with os.path.dirname(__file__)
Get your relative path with os.path.join(os.path.dirname(__file__), "Pickles", f"{name}_{date.strftime('%y-%b-%d')}")
it gives you:
with open(os.path.join(os.path.dirname(__file__), "Pickles", f"{name}_{date.strftime('%y-%b-%d')}"), 'rb') as f:
obj = pickle.load(f)

How to fix FileNotFoundError?

I have encountered a problem while trying to open a .txt file from the same directory where my source code is located.
When I tried to open the file like this:
with open("pi_digits.txt") as file_object:
contents = file_object.read()
print(contents)
I failed.
I also failed when I typed the whole path:
with open("Users\lukas\Documents\python_work\chapter_10") as file_object:
contents = file_object.read()
print(contents)
But when I typed:
with open("\\Users\\lukas\\Documents\\python_work\\chapter_10\\pi_digits.txt") as file_object:
contents = file_object.read()
print(contents)
I succeeded!
So my question is: Why can't I run the code without error when I enter the following code:
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
Thank you for your answers and sorry if my question was not well constructed.
#Sottvogel
In general, a python instance can be run from many different directories in your computer. One way to check where the running instance is:
import os
os.getcwd()
If you confirm that you are in the same directory of your file, try and see
what the following returns:
os.listdir()
In case your file doesn't appear in the returned string, then there has to be another problem. Make sure you checkout the docs as well.
Hope it helps!
the thing with paths is python is that they are referenced from where you launch the program (current working directory), not from where the file lives. So, I recommend making use of the os and pathlib packages to manage file paths properly.
Why can't I run the code without error when I enter the following code:
It depends from where you execute your python code.
Building on above, where you execute your code implies wether you need to use the file name or it's path.
If you execute your Python code in the same directory as the txt file, then you can use the file's name. However, if it is located elsewhere the script will need to access the file and hence, requires the path to it.

Unable to find text file

I am new to VSCODE. I opened a text file via vscode and entered some details. Where can I find the file?
f=open('story.txt','w')
f.write('my name is')
f.write('my age is')
f.close()
f=open('story.txt', 'r')
print(f.readline())
f.close()
this is the output
However I cannot find 'story.txt' in file explorer. I used another text editor and then error came as file not found. but when i reopened the file in vs code I was getting a proper output.
From the screenshot you supplied it looks like you are running the script from C:\Users\<YourName>, then this is where your story.txt file will be.
To specify another location you need to supply the open() method with a full path
Also, it's best practice to close the file before opening it again for reading. also, you might want to use a context manager to help you with this
If you want your file saved in the directory of your script, you can use os and __file__ to locate your script's directory and use that.
import os
my_dir = os.path.dirname(__file__)
new_path = os.path.join(my_dir, 'story.txt')
print(new_path)
os documentation
__file__ explained
When you run python script it will execute from the current working directory by defaut.
If you want to be sure where your file will be, you may pass the complete file path instead of file name only (eg: C:\\filepath\\filename.txt)
or you can move to the desired directory before read/write with os.chdir(filepath)
If you don't know where the script is running you can use os.getcwd() to get this directory from your code
import os
print(os.getcwd()) #will show the current working directory
os.chdir("c:\\") #will move to C:\ directory
f=open('story.txt','w')
f.write('my name is')
f.write('my age is')
f=open('story.txt', 'r')
print(f.readline())
f.close()
When you create a file with Python, the file will be create in the same folder as your script. So, if your folder all_python_scripts contains your script, your file story.txt will be created in this folder. Try to search your file in the script's folder.

How to load dependencies and subfolders in Pycharm?

I am so new with python and pycharm and i got confuse!!
When I run my project in pycharm it gives me an error about not finding the path of my file. The physical file path is:
'../Project/BC/RequiredFiles/resources/a_reqs.csv'
My project working directory is "Project/BC" and the project running file (startApp.sh) is there too. but the .py file that wants to work with a_req.csv is inside the "RequiredFiles" folder. There is the following code in the .py file:
reqsfile = os.getcwd() + "/resources/a_reqs.csv"
it returns: '../Project/BC/resources/a_reqs.csv'
instead of: '../Project/BC/resources/RequiredFiles/a_reqs.csv'
while the .py file is in "RequiredFiles" the os.getcwd() must include it too. but it does not.
The problem is that i can not change the addressing code. because this code works in another IDE and other people who work with the code in other platform or OS do not have any problem. I am working in mac OS and if i am not mistaken the code works with windows!!
So, how can i tell Pycharm (in mac) to see and load "RequiredFiles" folder as the subfolder of my working directory!!!
os.getcwd returns the current working directory of the process (which may be the directory where startApp.sh is located or another one, depending on the PyCharm's run configuration setting, or, if you start the program from the command line, the directory in which you execute the command).
To make a path independent on the current working directory, you can take the directory where your Python file is located and build the path from it:
os.path.dirname(__file__) + "/resources/a_reqs.csv"
From your question what I see is:
reqsfile = os.getcwd() + "/resources/a_reqs.csv"
Which produces: "../Project/BC/resources/a_reqs.csv", whereas your desired output is
"../Project/BC/resources/RequiredFiles/a_reqs.csv". Since we know os.getcwd is returning "/Project/BC/", then to get your desired result you should be doing:
reqsfile = os.getcwd() + "/resources/RequiredFiles/a_reqs.csv"
But since you want the solution to work with or without the RequiredFiles subdirectory you could apply a conditional solution, ie something like:
import os.path
if os.path.exists(os.getcwd() + "/resources/RequiredFiles/a_reqs.csv"):
reqsfile = os.getcwd() + "/resources/RequiredFiles/a_reqs.csv"
else:
reqsfile = os.getcwd() + "/resources/a_reqs.csv"
This solution will set the reqsfile to the csv in the RequiredFiles directory if the directory exists, and thus will work for you. On the other-hand, if the RequiredFiles directory doesn't exist, it will default to the csv in /resources/.
Typically when groups collaborate on projects, the maintain the same file hierarchy so that these types of issues are avoided, so you might want to consider moving the csv from /RequiredFiles/ to /resources/.

Python creates locked files on Ubuntu

I just ran into a problem, which is probably easy to fix - well, for you guys.
I try to create a directory, change to it, create a file in that directory and appened to that file. Everything works fine - except it marks the directory/file as locked and that isn't very convenient for me.
I am running my script as root, because I need to. When I run it normally that problem doesn't occur. I am on Ubuntu and down below is some example code plus a picture of the permissions of the given file, thanks!
import os
os.makedirs("foo", exist_ok = True)
os.chdir("foo")
with open("oof", "a") as f:
f.write("something" + "\n")
As you said you run the script as root so other users can not access this file.
You can change directory permissions:
from subprocess import call
call(['chmod', 'mode', 'path'])

Categories