accessing in the sub-folder of a folder in python - python-3.x

What I am trying to do is to access the images saved in my path that is E:/project/plane
but unable to get access.
I tried using glob.glob, all I'm getting is access to the subfolder not to the images inside the subfolder.
I also tried to take the name of the subfolder as an input combine it with the path but still can't get access to the folder.
Can anyone help me with how can I achieve this task?
here is my Python code:
import os
import glob
import cv2
path= "E:\project\%s"% input("filename")
print(path)
for folder in glob.glob(path + '*' , recursive=True):
print(folder)

Related

python3 get all .dart files from given directory

I would like to get all. .dart-files from a specific path.
This is what I've got so far:
import glob
import pathlib
libPathString = str(pathlib.Path.cwd().parent.resolve())
for path in glob.glob(libPathString + "/*.dart", recursive=True):
print(path)
Now this is already giving me all the .dart files in the libPathString - directories. But inside there Ive got a couple of folders that have .dart files and these sub-directories can also have subfolders and so on.
I set recursive = true but that doesn't seem to achieve going through the sub-directories.
What am I missing here?
I got it working! This is working as expected:
import glob
import pathlib
libPathString = str(pathlib.Path.cwd().parent.resolve())
for path in glob.glob(libPathString + "/**", recursive=True):
if(".dart" in path):
print(path)

Matplotlib created a temporary config/cache directory

Matplotlib created a temporary config/cache directory at /var/www/.config/matplotlib because the default path (/tmp/matplotlib-b33qbx_v) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
This is the message I'm getting in error.log file and 504 Gateway Time out error on browser .
Someone please help to resolve this issue.
Please check:
https://github.com/pyinstaller/pyinstaller/issues/617
I run matplotlib from the webserver and use:
os.environ['MPLCONFIGDIR'] = '/opt/myapplication/.config/matplotlib'
This dir should writable by the web server (e.g. www-data).
import os
os.environ['MPLCONFIGDIR'] = os.getcwd() + "/configs/"
before
import matplotlib
works for me

How can I allow a Python code to open "%temp%" folder for any user?

I have some users on my PC and I try to create a Python code to open %temp% folder, but the problem is that it works under my account only. When I use the same code on a different account it does not work on the same PC.
My folder path >> C:\Users\MyAccount\AppData\Local\Temp <<,
the problem error with this user 'MyAccount'
This is my code:
import webbrowser
webbrowser.open('C:\Users\MyAccount\AppData\Local\Temp')
I need to pass the correct userFolder to my code to work with.
Example:
my account the path >> **C:\Users\MyAccount\AppData\Local\Temp**
on different account >> C:\Users\ **?** \AppData\Local\Temp
**?** = it should be the name of the user.
Could you please advise me?
If pathlib is an option (comes with Python 3.4+) you can use
from pathlib import Path
Path.home() / 'AppData' / 'Local' / 'Temp'
if not, try
from os import path
path.expanduser('~/AppData/Local/Temp')

How to run PyPdf2 fileMerger within a shared folder on a network drive

I am trying to merge multiple files using PyPDf2 within a folder in my office's shared drive. However, my program never finishes running because I believe that it does not have permission to access the folder. Is there a way to allow access to it?
from PyPDF2 import PdfFileMerger
import os
path = "H:\\Accounting\\ME\\Attachments\\Pdf"
pdf_files = ["\\file1.pdf", "\\file2.pdf"]
merger = PdfFileMerger()
for files in pdf_files:
merger.append(path + files)
if not os.path.exists(path + "\\newMerged.pdf"):
merger.write(path + "\\newMerged.pdf")
merger.close()
print("done")

Add a folder with ~20K of images into Google Colaboratory

I am working with cat breeds recognition with Keras and try to use Google Colaboratory for training on GPU. When I worked in PyCharm, I used a path to folder with images:
data_dir = '//home//kate//Рабочий стол//барахло линух минт//more_breeds_all_new'
And I can not understand, how can I download a folder with 19500 images to Colab instead loading pictures there one by one like Google offers in it's notebook.
I also have a folder with these images on Google Drive, but I also do not know how to use it as a full folder with it's path.
First: zip image folder in .zip .tar format,example folder_data.zip
and sync or upload it( folder_data.zip) to Google Drive.
Get google drive file_id of zip file( folder_data.zip) like 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
Second:
I recommend you use Pydrive to download your file from google drive to colab notebook VM . I download 500MB dataset for 5s.
1. install Pydrive
!pip install PyDrive
2. OAouth
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
code for download file from google drive
fileId = drive.CreateFile({'id': 'DRIVE_FILE_ID'}) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
print fileId['title'] # folder_data.zip
fileId.GetContentFile('folder_data.zip') # Save Drive file as a local file
Finaly: unzip it to folder, example in here is
!unzip folder_data.zip -d ./
list file look like it
folder_data.zip
folder_data/
Cheer Mo Jihad

Resources