I have a program that reads in several subfolders of files.
I had previously written code that had :
path = 'C:/Users/Me/etc/etc/'
And then I would open several files by saying
file1 = path + str('file1.txt')
How do I change the code so that it can be used by others? I am using Jupyter notebook and so __file __ and sys(argv[0]) don't seem to be working.
I have looked at these:
How to get an absolute file path in Python
How do I get the parent directory in Python?
Use get current working directory,
def join_current_dir(file):
"""Join filepath with current file directory"""
cwd = os.getcwd()
return os.path.join(cwd, file)
Related
I have saved a file. I have not given a path to this. In the project folder, nowhere can I find the file.
Is there any method that will tell me where the default path is where the file has gone?
model_file = "test.h5"
model.save(model_file, overwrite=True)
print(os.getcwd()) will print the working directory, where relative paths start from (and where a relative filename such as foo.txt will end up in).
I am doing a project using python in which I got stuck at a point where I want to access some text files which are saved outside the project directory.
The path where my text files are saved:
C:\Users\saqibshakeel035\Desktop\Scientific Project Lithim battery project\text_file_r_w
The path of my python project:
C:\Users\saqibshakeel035\PycharmProjects\Tutorial_1
I want to open/read my text files (external > not included in python project folder)
I already know the Reading/writing etc etc within the same folder where the python project .py file is present but struggling with the different paths.
I tried:
import os
from os import path
print("Your cunrrent directory is : %s" %path.curdir)
strpath = r"C:\Users\saqibshakeel035\Desktop\Scientific Project Lithim battery project\text_file_r_w"
print("Your current directory is %s: " %path.dirname(strpath))
print("Your current directory is : %s" %path.abspath(strpath))
This works fine and it shows my abspath where my text files are stored but when I try to read it with the following command
f = open("file1.txt","r")
It gives error that no such directory or file found
I suggest you try f = open("C:/text/to/path/file1.txt","r") or the code #Jaba has mention. Either works fine
Can you try using the full path to "file1.txt" in the open function.
f = open("Full_path_to_file1.txt", "r")
Another option is to change the current directory,
os.chdir(path)
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/.
I have written the Python script below to find the last modified date of a file:
for root, dirs, files in os.walk(folder_path):
for f in files:
last_modified = datetime.fromtimestamp(os.path.getmtime(f)).day
#do something
This code works fine for normal files like .txt files. However, if it encounters files with extensions like .tar.gz or .run, it throws an error saying "No such file or directory" when finding last_modified.
* EDIT *
I realized that, if I change the code as below, it works fine.
for f in files:
name = os.path.join(root, f)
last_modified = datetime.fromtimestamp(os.path.getmtime(name)).day
#do something
But why does this happen?
os.walk return filenames relative to the folder it is walking. E.g. if it is walking a directory called foo with a file called bar, it will return bar, not foo/bar. However, since your script (presumably) is not in the folder os is walking, it needs an absolute path, or a path relative to itself. Therefore, you must join the folder path to the filename, so that python can find the file.
As the docs say:
Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
Why it happens to work with the text files I'm not sure. Do you have text files with the same name in the folder containing your script?
I'm having a problem with a program in which I have to load images and pickled objects: my Python software doesn't appear to be looking in the location of the program. I have my program in a folder called "King's Capture," and my images in a folder within "King's Capture" labeled "data." I want to have python find the files no matter where I place the folder "King's Capture." It seems to me that python should already be looking in the folder where the program itself is, but it apparently isn't. How should I go about this?
You can access the path of the current script file via the special variable __file__. So try this within your main program script:
import os
// ...
data_dir = os.path.join(os.path.dirname(__file__), 'data')
Try this
import sys, os
ROOT = os.path.dirname(os.path.abspath(__file__))
directory = ROOT + os.path.sep + 'data'
for eachFile in os.listdir(directory):
fileName = directory + os.path.sep + eachFile
print fileName