Create local files with python in Django - python

I want to create files with the extension .txt, .csv, .bin, etc. so the ideal would be with import os I think or if there is some other better way.
But I don't want to save those in the DB they are to consult the once or sporadically, that is, locally are uploaded to the server and then it is replaced and that's it.
In a simple way, I create the files I need, upload them and replace them when I need something that will not be so common.
The directory for these local files would be /app/files/.

I managed to create files using directory path, something like
imports
def createFile (self, my_data, nameFile):
directory = "./my_app/folder/" + nameFile + ".txt" # PATH directory
my_File = open (directory, "w")
my_File.write (my_data)
my_File.close ()
return true
In itself the important thing is import os and write well the directory where the file will be created. No need to modify anything else.
In the Django documentation I saw something in the import of some classes but in my case this was enough, on the other hand now that I write this answer from django.core.files import File which as seen is the equivalent of import os
I tried to use these
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.core.files.storage import FileSystemStorage
As I do not have time and I had problems with these, I went with the easy
PS: maybe then use from django.core.files import File instead of import os as it seems to be the same, if so I update the answer.

Related

Iterate directory without joining file with dir (os.join)

I want to know if there is a package that lets me avoid typing the os.path.join every time I'm opening a file, and have the handler already contain this info.
From this:
import os
top = '~/folder'
for file in os.listdir(top):
full_file_path = os.path.join(top, file)
To:
import package
top = '~/folder'
for file in package.listdir(top):
full_file_path = file
In short: I recommend option 2
Option 1: A potential quick and dirty approach:
If your goal is to then open the files and you don't want to have to join, a trick could be to change your working directory:
import os
os.chdir(os.path.expanduser('~/folder'))
for file in os.listdir('.'):
# do something with file
note that file will not contain the full path to your file, but since you changed your local dir (equiv do cd), you will be able to access the file with just its name.
Option 2: Cleaner approach - pathlib:
Aside from that, you can also look into pathlib which is a module that provides nice path utilities.
For example:
import pathlib
path = pathlib.Path('~/folder').expanduser()
for file in path.glob('*'):
# do something with file
So you get a better idea:
In [23]: path = pathlib.Path('~/Documents').expanduser()
In [24]: [i for i in path.glob('*')]
Out[24]:
[PosixPath('/home/smagnan/Documents/folder1'),
PosixPath('/home/smagnan/Documents/folder2'),
PosixPath('/home/smagnan/Documents/file1')]
You should be able to define an alias on your import:
from os.path import join as pjoin
Or simply import it with from ... import, though join is a bit overloaded as a term, given it's implication for strings.
from os.path import join
You can create an alias to simplify the usage of a function:
J = os.path.join
## -or- ##
import from os :: path.join as J
J(top, file)

FILE (.json) NOT FOUND

This is my directory where i have activated a virtual environment:
I'm working on a flask project to create a rest API, and I have a JSON credential file (google vision file), but when I run the code it says file not found even if it's in the same directory. I've activated a virtualenv for this particular project. mainone.py is the code I'm trying to run.
This is the error I am getting:
"File {} was not found.".format(filename)
google.auth.exceptions.DefaultCredentialsError: File date_scanner.json was not found.
And this is the code block where I am using accessing the particular file:
from flask import Flask,request,jsonify
import os,io,re,glob,base64
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'date_scanner.json'
client=vision.ImageAnnotatorClient()
This is likely because the "working folder" of the Python process is not the same as where the file is located. There is a small (albeit convoluted) way to generate filenames which will always work:
Use __file__ to get the filename of the Python file where this is called.
Strip the filename from that path using os.path.dirname
Append the filename that you want to open using os.path.join
from os.path import dirname, join
def get_filename(filename):
here = dirname(__file__)
output = join(here, filename)
return output
Some food for thought
However, in this case there is something you should be careful about: The file contains security credentials and should not live in your source code. So while the above example will solve the immediate problem, the security of this should be addressed.
The best way for this is to externalise the filename of the credentials file into a config file. Say, for example you would use a .ini file as config file, you could have something like this in your code somewhere:
config = configparser.ConfigParser()
config.read('config.ini')
vision_creds_file = config.get('google_vision', 'filename')
and then place the following into config.ini:
[google_vision]
filename = /path/to/filename.json
This still requires you to place the config.ini file somewhere which should be documented in your application, as you still cannot add this to the source code (maybe a sample file with defaults).

Opening a CSV from a Different Directory Python

I've been working on a project where I need to import csv files, previously the csv files have been in the same working directory. Now the project is getting bigger so for security and organizational resaons I'd much prefer to keep them in a different directory.
I've had a look at some other questions asking similar things but I couldn't figure out out how to apply them to my code as each time I tried I kept getting the same error message mainly:
IOError: [Errno 2] No such file or directory:
My original attempts all looked something like this:
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
login = csv.reader(file('/~/Projects/bmm_private/login_test.txt'))
I changed the path within several times as well by dropping the first / then then the ~ then the / again after that, but each time I got the error message. I then tried another method suggested by several people by importing the os:
import os
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
path = r'F:\Projects\bmm_private\login_test.txt'
f = os.path.normpath(path)
login = csv.reader(file(f))
But I got the error message yet again.
Any help here would be much appreciated, if I could request that you use the real path (~/Projects/bmm_private/login_test.txt) in any answers you know of so it's very clear to me what I'm missing out here.
I'm pretty new to python so I may struggle to understand without extra clarity/explanation. Thanks in advance!
The tilde tells me that this is the home folder (e.g. C:\Users\<username> on my Windows system, or /Users/<username> on my Mac). If you want to expand the user, use the os.path.expanduser call:
full_path = os.path.expanduser('~/Projects/bmm_private/login_test.txt')
# Now you can open it
Another approach is to seek for the file in relative to your current script. For example, if your script is in ~/Projects/my_scripts, then:
script_dir = os.path.dirname(__file__) # Script directory
full_path = os.path.join(script_dir, '../bmm_private/login_test.txt')
# Now use full_path

How to read a JSON static file in web2py

I'm putting together my first web2py app, and I've run into a bit of a problem. I have some data stored in static/mydata.json that I'd like to access in a couple places (specifically, in one of my models, as well as a module).
If this were a normal python script, obviously I'd do something like:
import json
with open('/path/to/mydata.json') as f:
mydata = json.load(f)
In the context of web2py, I can get the url of the file from URL('static', 'mydata.json'), but I'm not sure how to load mydata - can I just do mydata = json.load(URL('static','mydata.json')? Or is there another step required to open the file?
It's advisable to use os.path.join with request.folder to build paths to files.
import os
filepath = os.path.join(request.folder,'static','mydata.json')
From that point on, you should be able to use that filepath to open the json file as per usual.
import os
filepath = os.path.join(request.folder,'static','mydata.json')
mydata = json.load(filepath)

Write to new non-existent file in same directory, Python 2.7

I'm trying to read a document and write it into a new file (that doesn't exist) in the same directory as the read file
An example would be
test_infile='/Users/name/Desktop/test.txt'
in_file=open(test_infile,'r')
data=in_file.readlines()
out_file=open('test_outfile.csv','w')
out_file should create a new file called test_outfile.csv in the directory /Users/name/Desktop/test_outfile.csv
I worked with the os module and got
def function(test):
import os
in_file=open(test,'r')
dir,file=os.path.split(test)
temp=os.path.join('output.csv')
out_file=open('output.csv','w')
test_file='/Users/name/Desktop/test.txt'
function(test_file)
it runs but nothing is created ?
Use the os.path.split function to split the directory path from the file name, and then the os.path.join function to stitch path constituents together:
import os
dir, file = os.path.split(test_infile)
out_file_name = os.path.join(dir, 'test_outfile.csv')
Please make sure you read the documentation of these functions, and the os.path module in general, since it's very useful.

Categories