Assigning a filepath to a variable in Python 3
Are you affecting datadir with a path you wrote on 2 rows in your code ?
Try to put line 7 and 8 on the same row or change the quotes like that:
import numpy as np
import matplotlib.pyplot as py
import os
import cv2
from pathlib import Path
datadir = Path("""/home/cryptoaniket256/Desktop/opencv-3.4.1/project/Resize""")
fileToOpen = datadir/"practice.txt"
f = open(fileToOpen)
print(f.read())
Aniket Sonavane
An IT Engineer from Mumbai University and also a Machine Learning enthusiast. I love to work on projects related to Machine and Deep Learning and wish to begin my career in the same. Always ready to learn and keen on enhancing my knowledge related to IT f-CS fundamentals.
Updated on June 04, 2022Comments
-
Aniket Sonavane almost 2 years
I am trying to convert few camera-clicked images of handwritten Gujarati characters to the form of MNIST dataset as I intend to pass the Gujarati handwritten characters images to the MNIST deep learning model. And as part of that, I'm trying to assign a file path to a variable named "datadir". But when executing the below code in Ubuntu 16.04, the terminal throws the error which looks like this: File "gujaratinn.py", line 7 datadir = /home/cryptoaniket256/Desktop/opencv-3.4.1/project/Resize ^ SyntaxError: invalid syntax
Note that the name of the file is gujaratinn.py and all the camera-clicked images are stored in the Resize folder.
import numpy as np import matplotlib.pyplot as py import os import cv2 from pathlib import Path datadir = Path("/home/cryptoaniket256/Desktop/opencv- 3.4.1/project/Resize") fileToOpen = datadir/"practice.txt" f = open(fileToOpen) print(f.read())
-
Furkan Kambay over 5 yearsAlso do
os.path.dirname(__file__)
to get the script's directory instead of hardcoding it.
-
-
Aniket Sonavane over 5 yearsHello @NickNick! Actually, line number 7 and 8 are on the same line itself in the actual code.
-
Aniket Sonavane over 5 yearsHurray! The change of quotes solution worked @NickNick! Thank You, buddy, for the help.