Error in colab in finding directory and file - keras

img = load_img('train/cat/cat.12499.jpg')
I'm using colab with the same code that I used in Jupyter, but now I get the below error, although the directory and file there are in:
No such file or directory: 'train/cat/cat.12499.jpg'
I copied the directory in google drive!

I solved with these commands
from google.colab import drive
drive.mount('/content/drive')
and I've modified the code in this way:
img = load_img("drive/My Drive/app/train/cat/cat.12499.jpg") # t

Related

Pygame with PathLib?

I am creating a Python 3.9 game using Pygame. I want to bundle it as a .app file using py2app, but pygame needs Absolute Paths for the assets I'm using. I want to use PathLib to find the base directory, but the code I have results in the FileNotFoundError: No such file or directory. error.
Here is a sample of my code:
import os.path
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
ASSETS_DIR = os.path.join(BASE_DIR, 'assets')
# Load images
RED_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_red_small.png'))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_green_small.png'))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_blue_small.png'))
Please can someone tell me what my issue is?
I used an extra .parent
That was the issue so it was looking in the wrong directory.

please how do i fix this issue..? i am trying to make a webscraper

No such file or directory: 'hackathon.html'
this is an error while running the code on spyder
from bs4 import BeautifulSoup
with open('hackathon.html','r') as html_file:
content=html_file.read()
print(content)
\\hackathon.html is saved on my desktop
Put the file in the directory you are running your script or give open() the full path information.

How to access files downloaded from kaggle into a Colaboratory notebook?

I am having some difficulties with manipulating multiple files in a Colaboratory Notebook downloaded to the /content directory in my google drive. So far, I have successfully downloaded and extracted a kaggle dataset to a Colaboratory Notebook using the following code:
!kaggle datasets download -d iarunava/cell-images-for-detecting-malaria -p /content
!unzip \cell-images-for-detecting-malaria.zip
I was also able to use Pillow to import a single file from the dataset into my Colaboratory session (I obtained the filename from the output produced during the extraction):
from PIL import Image
img = Image.open('cell_images/Uninfected/C96P57ThinF_IMG_20150824_105445_cell_139.png')
How can I access multiple extracted files from /content without knowing their names in advance?
Thank you!
After some further experimentation, I found that the python os module works similarly in Colab Notebooks as it does on an individual computer. For example, in a Colab Notebook the command
os.getcwd()
returns '/content' as an output.
Also, the command os.listdir() returns the names of all the files I downloaded and extracted.
You can use glob. glob.glob(pattern) will match all files that match the pattern. For example the code bellow will read all the .png files in the image_dir.
png = glob.glob(os.path.join(img_dir, '*.png'))
png = np.array(png)
png will contain a list of filenames.
In your case you can use:
png = glob.glob('cell_images/Uninfected/*.png')
png = np.array(png)

How to extract a zip file using python 3

How to extract a zip file using python when zip file present in different directory where script file present.
I try this ,but i got error because source path is not accepted ,try to solve me this problem.
from zipfile import ZipFile
def func(source, target):
with ZipFile('source', 'target'):
ZipFile.Extractall('target')
Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
For just unpacking, shutil should suffice:
import shutil
shutil.unpack_archive('path-to-zipfile')
You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try
import os
print(os.getcwd())
zip - Unzipping files in python
relative-paths-in-python

Python cant list files in a directory

I follow some articles regarding on how to list files in a directory using the below code :
import os, sys
path = "/python 3.6.4"
dirs = os.list(path)
for file in dirs:
print file
Note: The .py file in which the above code is on the same directory as the path.
Im running it using a command prompt. Any idea would be much appreciated. Thanks!

Resources