Opening a dynamic file in Django not in the project directory - python

So I am working on an app that allows users to upload CSV files and then generate graph data corresponding to their files. I have it working in development only when the files that are uploaded are sitting in the project's current working directory.
I discovered that the reason behind this is because in my graph view I am opening the file like so:
data_file = open(new_file, 'rb')
Open expects to find the file within the project directory. If I attempt to upload a file outside of that directory it throws this error:
Errno 2] No such file or directory: 'test.CSV'
I've read about os.path.expanduser and have tried:
data_file = open(os.path.expanduser('~' + new_file), 'rb')
but without success. The above code tries to find the file in C:/Users/test.csv.
Any suggestions on how I can achieve this would be greatly appreciated.
EDIT
My current attempt is now:
file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'Data_Files')
data_file = open(os.path.join(file_upload_dir, new_file), 'rb')
And the error is:
File b'test.CSV' does not exist
Data_Files is a folder within my Media folder.

You need join ~, and file name with directory separator (os.sep). Using os.path.join will do it for you.
data_file = open(os.path.expanduser(os.path.join('~', new_file)), 'rb')

Uploaded files, as well as file generated by your application, have nothing to do in the project's directory (=> source code). You have a setting for where to store them (settings.MEDIA_ROOT), and you have a models.FileField to remember where they are stored and how to access them.

Related

Streamlip app not searching files in the good directory

I am trying to run a Streamlit app importing pickle files and a DataFrame. The pathfile for my script is :
/Users/myname/Documents/Master2/Python/Final_Project/streamlit_app.py
And the one for my DataFrame is:
/Users/myname/Documents/Master2/Python/Final_Project/data/metabolic_syndrome.csv
One could reasonably argue that I only need to specify df = pd.read_csv('data/df.csv') yet it does not work as the Streamlit app is unexpectedly not searching in its directory:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/myname/data/metabolic_syndrome.csv'
How can I manage to make the app look for the files in the good directory (the one where it is saved) without having to use absolute pathfiles ?
you can use os.getcwd() to get the Current Working Directory. Read up on what this means exactly, fe here
See the sample script below on how to use it. I'm using os.sep for OS agnostic filepath separators.
import os
print(os.getcwd())
relative_path = "data"
full_path = f"{os.getcwd()}{os.sep}{relative_path}"
print(full_path)
filename = "somefile.csv"
full_file_path = f"{full_path}{os.sep}{filename}"
print(full_file_path)
with open(full_file_path) as infile:
for line in infile.read().splitlines():
print(line)
In which directory are you standing when you are running your code?
From your error message I would assume that you are standing in /Users/myname/ which makes python look for data as a subdirectory of /Users/myname/.
But if you first change directory to /Users/myname/Documents/Master2/Python/Final_Project and then run your code from there I think it would work.

Onedrive API with python : path and upload

I use python and I would like to upload a file in a specific folder on Onedrive
I have an existing XLSX file in the folder data/project1/sample.xlsx
and I want to copy this file in the folder copy/all_files/sample.xlsx
(if the file already exists it can be replaced)
I execute the code bellow and the file is uploaded on my root folder)
returned_item = client.item(drive='me', id='root').children['sample.xlsx'].upload('sample.xlsx')
but I can't specify a specific folder to upload my file
Could you please tell me how it's possible ?
if I specify path like that, it doesn't work
returned_item = client.item(drive='me', id='root').children['copy/all_files/sample.xlsx'].upload('sample.xlsx')
I used the code and configuration from here https://developer.microsoft.com/en-us/graph/quick-start
You almost had it, you just need to change id to path in the client.item()
So assuming the folder is called all_files, you can upload it like this:
returned_item = client.item(drive='me', path='all_files').children['sample.xlsx'].upload('sample.xlsx')

Python save file to root subfolder

I am trying to set the folder_out to the subfolder where the source .csv is found.
So I have many folders and subfolders in the main Processing folder.
I want to save the the .csv file in the same folder as where it has found the file.
When I use the root, with pathlib, is that possible?
And, I am getting now back IOError: [Errno 13] Permission denied: 'D:\Processing\DG_Boeblingen-..... etc.
So it found the file, but can't write.
I am in Python 2.7 and using 'wb' to write.
how I set the Path and rb an wb, is using wb and rb, correct?
folder_in = Path(r'D:\Processing')
folder_out = Path(r'.')
folder_in_traj = Path(r'D:\Processing')
folder_out_traj = Path(r'.')
for incsv in folder_in.iterdir():
outcsv = folder_out.joinpath('0new'+incsv.name)
with open(str(incsv), 'rb') as input, open(str(outcsv), 'wb') as output:
You are trying to save a file in root directory for which you would need sudo prviliges so if you execute the python script as super user then you should not see this issue.
I am kind of confused as to what you are trying to do here. Are you trying to output the CSV to root? In that case I think you are using Path(r'root') wrong. If you look at the documentation for pathlib, there is a class called PurePath with a method called root. You can use this to return the root.
Passing in root to Path will just return root as the path. You can try using . instead of root which might resolve to the root.

File path in python

I'm trying to load the json file but it gives me an error saying No such file or directory:
with open ('folder1/sub1/sub2/sub2/sub3/file.json') as f:
data = json.load(f)
print data
The above file main.py is kept outside the folder1. All of this is kept under project folder.
So, the directory structure is Project/folder1/sub1/sub2/sub2/sub3/file.json
Where am I going wrong?
I prefer to point pathes starting from file directory
import os
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, 'relative/path/to/file.json')
with open(file_path, 'r') as fi:
pass
this allows not to care about working directory changes. And also this allows to run script from any directory using it's full path.
python script/inner/script.py
or
python script.py
I would use os.path.join method to form the complete path starting from the current directory.
Something like:
json_filepath = os.path.join('.', 'folder1', 'sub1', 'sub2', 'sub3', 'file.json')
As always, an initial slash indicates that the path starts from the root. Omit the initial slash to indicate that it is a relative path.

How to open a file within pyramid for reading?

I have a typical Pyramid web application setup. The application directory (I don't know what this directory is called in Pyramid?) contains the static, templates and ini.py files. In this directory I also created a directory called static_content that I use to store some special report templates.
In my view code I am using something like this to read the files in the sub-directories of the static_content directory:
f = open("/static_content/abc/report_template.tpt" , "r")
Then over in my init.py file I added a line:
config.add_static_view("static_content", "static_content")
I get an IO Error.....how do I fix this?
Regards,
Mark Huang
f = open("/static_content/abc/report_template.tpt" , "r")
A leading slash in a file's path means you're giving it the full path (the file is at this exact location). If you want a relative path, take off the leading slash:
f = open("static_content/abc/report_template.tpt" , "r")
This tells it to follow that path from the current directory.
You may want to look at this question in order to build a relative path from the script file.

Categories