I am working with a python script on a server with the following hierarchy:
DreamteamPy (folder)
pictest.py
assets (folder)
pictures (folder)
31.jpg
picture2.jpg
picture3.jpg
The complete path of the python file is
http://www.cytosine.nl/~owe4_pg3/Rogier/DreamteamPy/pictest.py
And one of the pictures:
http://www.cytosine.nl/~owe4_pg3/Rogier/DreamteamPy/assets/pictures/31.jpg
How can I get all of the files in the pictures folder?
I've tried things like
import os
def index():
filelist=[]
path= "http://www.cytosine.nl/~owe4_pg3/Rogier/DreamteamPy/assets/pictures"
for filename in os.listdir(path):
filelist.append(filename)
but to no avail.
What you call is path isn't a path but a HTTP URL. os.listdir() needs a path on the local file system. Which in your case most likely is something like /home/owe4_pg3/html/Rogier/DreamteamPy/assets/pictures unless it is not a typical Linux installation where a web server is configured to map ~username at the start of the path part of an URL to /home/username/html.
os.listdir() already returns a list, so it doesn't make much sense to copy the elements one by one into another list.
import os
PICTURES_PATH = '/home/owe4_pg3/html/Rogier/DreamteamPy/assets/pictures'
def index():
filenames = os.listdir(PICTURES_PATH)
Related
So this is a question about how to handle settings files and relative paths in python (probably also something about best practice).
So I have coded a smaller project that i want to deploy to a docker image and everything is set up now except when I try to run the python task (Through cron) I get the error: settings/settings.yml not found.
tree .
├───settings
│ └───settings/settings.yml
└───main.py
And am referencing the yml file as
open('settings/settings.yml', 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
I can see this is what is causing the problem but am unsure about how to fix it. I wish to reference the main file basically by using the entry_points from setuptools in the future so my quick fix with cd'ing before python main.py will not be a lasting solution.
Instead of hardcoding a path as a string, you can find the directories and build the file path with os.path. For example:
import os
import yaml
current_dir = os.path.dirname(os.path.abspath(__file__))
settings_dir = os.path.join(current_dir, "settings")
filename = "settings.yml"
settings_path = os.path.join(settings_dir, filename)
with open(settings_path, "r") as infile:
settings_data = yaml.load(infile)
This way it can be run in any file system and the python file can be called from any directory.
I am trying out static web pages service from Azure. But I need to access a list of files (for example images stored on the server) from the client side. I am trying to implement a Function that will iterate over the files in a particular folder and return a list of names.
The problem is that I simply cannot access those files. It can't find them from the context in which the Function runs as if they are stored on another machine.
I now use this function to print the folders and files accessible to the python.
This is the function I used called GetResources:
import logging
import os
import sys
import azure.functions as func
import json
import os
def showFolderTree(path):
show_files=True
indentation=1
file_output=False
tree = []
result=""
if not show_files:
for root, dirs, files in os.walk(path):
level = root.replace(path, '').count(os.sep)
indent = ' '*indentation*(level)
tree.append('{}{}/'.format(indent,os.path.basename(root)))
if show_files:
for root, dirs, files in os.walk(path):
level = root.replace(path, '').count(os.sep)
indent = ' '*indentation*(level)
tree.append('{}{}/'.format(indent,os.path.basename(root)))
for f in files:
subindent=' ' * indentation * (level+1)
tree.append('{}{}'.format(subindent,f))
for line in tree:
result+=line+"\n"
return result
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
# logging.info('Python HTTP trigger function processed a request.') --> where is this logged?
try:
errors=context.function_directory+"\n"
except Exception as e:
error="context error\n"
try:
errors+=os.path.dirname(os.path.realpath(__file__))+"\n"
errors+=os.getcwd()+"\n"
errors+=showFolderTree("/")
except Exception as e:
errors+=e
return func.HttpResponse(errors,status_code=200)
This function returns:
/home/site/wwwroot/GetResources
/home/site/wwwroot/GetResources
/home/site/wwwroot
/
app/
.bash_logout
.bashrc
.profile
site/
wwwroot/
.funcignore
requirements.txt
proxies.json
.gitignore
host.json
GetResources/
function.json
sample.dat
__init__.py
__pycache__/
__init__.cpython-38.pyc
.python_packages/
lib/
site-packages/
azure/
functions/
...
but I cannot find my files in the list.
Observations:
The Python code runs on a linux environment
I tried a similar code with C# and a Windows environment running on Azure
I tried placing a folder of assets with pictures in the api folder in which the function resides. Still could not find the files.
What am I doing wrong?
Note: I have a student subscription
Side issue: I cannot find anywhere the logs generated by the Function (generated by the logging function)
While Azure Static Web Apps act as a proxy to Azure Functions for APIs, the Functions themself are hosted separate to the Static Web App and don't share the filesystem.
So, instead of having these files pushed alongside the Web App, they should be stored in Blob Storage and from Functions, you could leverage Blob Storage Bindings to read/write them. To list files in a container, you need to bind to BlobContainerClient and use a method like BlobContainerClient.GetBlobs.
Duplicate of my answer on Microsoft Q&A
I'm trying to run this specific python script to create .m3u files inside subfolders but it's not working and give me an error, any help is welcome.
The m3u file is a simple tracklist of the subfolder content with specified extensions and named after the subfolder, like this (extensions .aaa and .bbb are just examples):
Folder 1 (contains 'File 1.aaa', 'File 2.aaa', etc)
Folder 1.m3u generated inside Folder 1 with this list
File 1.aaa
File 2.aaa
Folder 2 (contains 'File 1.bbb', 'File 2.bbb', etc)
Folder 2.m3u generated inside Folder 2 with this list
File 1.bbb
File 2.bbb
Here is the script called makem3u.py (not mine, I don't know much about python):
#!/usr/bin/python
"""This script will create an m3u file with a tracklist of each .aaa or .bbb
found in a subdirectory, then name the m3u after the subdirectory. This helps
with multiple disks for emulation.
"""
import os
import os.path
EXT = ['.aaa', '.bbb']
cwd = os.getcwd()
dirs = os.listdir(cwd)
for d in dirs:
contents = os.listdir(os.path.join(cwd, d))
disks = [
os.path.join(d, f) for f in os.listdir(os.path.join(cwd, d))
if os.path.splitext(f)[-1] in EXT
]
if disks:
with open(os.path.join(cwd, '{d}.m3u'.format(d=d)), 'wb') as m3u:
m3u.writelines(['{disk}\n'.format(disk=disk) for disk in disks])
I get this error when I try to run it:
Traceback (most recent call last):
File "makem3u.py", line 16, in <module>
contents = os.listdir(os.path.join(cwd, d))
NotADirectoryError: [WinError 267] The directory name is invalid: 'path\\to\\file\\makem3u.py'
makem3u.py is inside a folder with the subfolders mentioned
Windows 10, Python 3.8.5, python is installed properly and the PATH is in enviroment variables and I can run other scripts just fine
What I'm doing wrong and how can I fix this? Is there a non-python alternative like create a .bat file to do that? Sorry for so many questions, I'm a noob in these things. Thank you in advance!
Also, is there a way to batch zip all the files in the subfolders (the generated m3u + original files) and name each zip after that subfolder? This is an extra, but would be helpful if possible
I have a script (dump_ora_shelve.py) which retrieves the data from shelve-storage by specifying the key, e.g.:
def get_shelve_users(field):
import shelve
db = shelve.open('oracle-shelve')
for key in db:
if key == field:
return db[key]
db.close()
The data is retrieved just fine:
print(get_shelve_users('db_users'))
> {'SYS': 'sysdba'}
print(get_shelve_users('oratab'))
> ['orcl:/u01/app']
There is another script which should do the same thing (retrieve the data with key specified) that has dump_ora_shelve imported, but the value returns is Null:
from before_OOP.dump_ora_shelve import get_shelve_users
print(get_shelve_users('db_users'))
> Null
print(get_shelve_users('oratab'))
> Null
The file being imported is located one level above from the file it is importing to.
Please note if I copy both files to the same location import and then function works just fine.
You could provide the full pathname to shelve.open. Remember that inside a module, __file__ is the path of where the source file resides. So you can use that to construct the full pathname.
Typically you will have something like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Use os.path.join to concatenate the directory and the filename. Note the use of os.path.dirname and os.path.abspath.
So you can say:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
shelve_db = os.path.join(BASE_DIR, 'oracle-shelve')
db = shelve.open('oracle-shelve')
This assumes that the orache-shelve file is in the same folder as the module in which the get_shelve_users function is (dump_ora_shelve.py).
Don't forget the __file__. That is what makes the whole thing tick, i.e. makes your program insulated from whatever its current directory is.
When running the second script your working directory will be the directory where that script is located. That working directory is kept even when you import and use a file from a different package/directory.
So if your dump_ora_shelve.py script and shelve is located in a different directory/package it will not open the correct file.
If you provide the full path to 'oracle-shelve' in dump_ora_shelve.py it should work.
Update:
In your 'dump_ora_shelve.py' file:
ABS_DIR = os.path.dirname(os.path.abspath(__file__))
This gives you the absoulte path of the directory of ''dump_ora_shelve.py'. Join with name of your DB:
shelve_db = os.path.join(ABS_DIR, 'oracle-shelve')
And finally:
db = shelve.open(shelve_db)
This assumes that your 'oracle-shelve' is in the same directory as 'dump_ora_shelve.py'
I have 2 questions(I could not find answer on stackoverflow):
First question:
I added run_command.bat file to:
DjangoProj/
---DjangoApp/
------views.py
------run_command.bat
In method save_logs in DjangoProj/DjangoApp/views.py I tried:
def save_logs(request):
choosenMachines = request.GET.getlist('mvsMachine')
(data,errors) = subprocess.Popen(r'run_command.bat' + str(choosenMachines), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()
But I got this error:
the run_command.bat is not recognize as external or internal command,
exec file or batch file
I suppose that Django is currently in another path(the question is which)
And second question:
Where is saved txt file created by method from DjangoProj/DjangoApp/views.py
def set_parameters_on_ftp(request):
with open('start_task.txt', 'w') as f:
for command in commands:
f.write(command+'\n')
return
It suppose it should be in: DjangoProj/DjangoApp/*
For the first question:
import os
#Set myPath variable to the path of the file being executed
myPath = os.path.dirname(os.path.abspath(__file__))
#Change current working directory to myPath
os.chdir(myPath)
#Or change current working directory to a subdirectory of myPath
os.chdir(os.path.join(myPath, 'subFolder'))
For the second question:
import os
#Check the current working directory. The txt file is getting saved here.
os.getcwd()
#This can be changed by changing the working directory as described in the answer to the first question.
EDIT: Changed the os.chdir() syntax error in the first part.
Your guess was true.
Django current running path is not in your project folder.
In my testing it was in C:\Python27
you must give exact path or use PROJECT_ROOT variable in settings file.
Have fun