django request absolute file path - django-request

using django requests, I am trying to get the absolute file path of original, uploaded file. The reason is I want to modify the original file so just the filename or some "media root" location just isn't enough.
I tried doing something like
request.FILES['file'].name
but that just gives me the name, not a path (neither absolute nor relative). google didn't really help, filenames are common, but path seems to be a special case. I hope it's even possible XP.
Thx in advance!

Try this.
myfile = request.FILES['filename']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
print(uploaded_file_url)
Also make sure you add the 'django.core.files.storage.FileSystemStorage' under Middleware_classes in settings.py file

Related

How to open a file in TYPO3 extbase extension controller?

I need to read an file inside of the extension controller, here as example my extension key is myext_key and the file I want to open is a JSON file data.json in the Resources/Private/JSON directory. My research gave me, that the best way to open a file wouldn't be with file_get_contents($path), instead with \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($path).
So I tried it with the following code, but that didn't work:
$content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL('EXT:myext_key/Resources/Private/JSON/data.json');
Thanks for all help!
Not sure if anything is wrong with file_get_contents(), other than memory implications, because file_get_contents() essentially assigns a variable with the whole file content. As long as you are dealing with a small .json file is small you should be in the safe.
The method to get the absolute file name of a file inside an extension directory is getFileAbsFileName()
$fileContent = file_get_contents(
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:myext_key/Resources/Private/JSON/data.json')
);

MatLab - accessing subfolders of folders saved as variables

I have the following code which can create a directory within a selected folder:
photos_dir = 'C:\Users\Bob\Photos';
mkdir(photos_dir,'Christmas 2015')
I'd like to then be able to save an image to this folder, I think using something like:
imwrite(img,Christmas 2015,'jpg')
However, this does not select the "Christmas 2015" folder which is in the "\Photos" directory. How can I make the image be written to this location?
First of all, you're going to have a syntax error since Christmas 2015 should at least be a string. But other than that, if you want to save a file in a particular location (other than the current working directory), you need to provide the full path to the file location.
To do this, you need to use fullfile to combine all of your directory and file names together into a full file path.
image_name = fullfile(photos_dir, 'Christmas 2015', 'yourphoto.jpg');
imwrite(img, image_name, 'jpg')

Why do I need to change MATLAB path in order to read the files?

I am reading 50 files from a folder as follows:
list_of_files=dir(fullfile('/home/user/Desktop/MTP/schemes/o33smnpimp/data/', '*.dat'));
My problem is until & unless I have same exact folder opened as path in MATLAB path (one above the path window) this command won't work. What is the reason behind this? Actually there are multiple schemes and every time I need to run a particular scheme, I have to go to the data folder of that particular scheme. How can it be solved?
The issue is that you can get the list of files using the full path like you have but you ALSO need to specify the full path when you use it. For example, try changing your code to:
baseDir = '/home/user/Desktop/MTP/schemes/o33smnpimp/data/'; % <--- will use this twice
list_of_files=dir(fullfile(baseDir, '*.dat'));
for ind = 1:length(list_of_files)
myFilenameFull = fullfile(baseDir, list_of_files(ind).name); % <---- must use fullfile here too!
D1 = getData(myFilenameFull, 'stuff');
end

How to write relative path in MATLAB?

I need to read a group of dat files so when I do this it is working all right.
list_of_files=dir(fullfile('/home/username/Desktop/Old/MTP/Generate/schemes/o33smnpimp/data/', '*.dat'));
The thing is I want to do this for number of schemes (like o33smnpimp) where every scheme folder has a data folder so I tried something like this but it's not working. What could be the problem?
list_of_files=dir(fullfile('../data/', '*.dat'));
My matlab file lies in o33smnpimp folder.
.. indicates the parent directory, . the current directory. your code looks in /home/username/Desktop/Old/MTP/Generate/schemes/ for the sub directory data, assuming your working directory is /home/username/Desktop/Old/MTP/Generate/schemes/o33smnpimp.
Use
list_of_files=dir(fullfile('./data/', '*.dat'));
or
list_of_files=dir(fullfile('data', '*.dat'));

Windows get short path to a file before creating it

I'm trying to get a short path of a file before the file is created. I found an example which can convert a path to a shortPath if the folder or file existed. However, is there a way to get short path before the file even exist?
Thanks for all the comments. I think the easiest way to solve this problem is:
if file DOES NOT existed:
generate shortPath
return shortPath
else:
touch a new file with exact name expected.
generate shortPath
remove temp file
return shortPath