I'm trying to get an image to show on the screen. I have the image file and code file in the same folder. I tried using the image file name and it said that there is no such file or directory. How do I do that?
I think you should try to write absolute path. you can use following code
import os
path = os.getcwd()
abs_path = path + img_name
Related
I am getting stuck when trying to iterate through files in a directory ('PDFS') with fitz from PyMuPDF.
The thing is, the code works when I am just doing document = "somepdf.pdf", but as soon as I insert a for loop and try to access files that way this error shows up:
filename, stream, filetype, rect, width, height, fontsize
RuntimeError: cannot open sample.pdf: No such file or directory
Here is the code:
for file in os.listdir('PDFS'):
if fnmatch.fnmatch(file, '*.pdf'):
document = file
doc = fitz.open(document)
Thank you for the help!
Your pdf files to open is under sub-directory PDFS, e.g. PDFS/sample.pdf, while your code fitz.open(document) is to open file under current working directory. So, a fix should be:
import fitz
import os
import fnmatch
for file in os.listdir('PDFS'):
if fnmatch.fnmatch(file, '*.pdf'):
document = os.path.join('PDFS', file)
doc = fitz.open(document)
Furthermore, a relative path PDFS is used, so you have to run the code right under the path where PDFS in, say /your/workspace/:
/your/workspace > python code.py
Otherwise,
/your > python workspace/code.py
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'PDFS'
So, a good practice is to
use full path if PDFS is just a user input path; otherwise,
use relative path to the script path
script_path = os.path.abspath(__file__)
project_path = os.path.dirname(script_path)
pdfs_path = os.path.join(project_path, 'PDFS')
I had the same issue. Its because PyCharm automatically installs the module named fitz whereas you need to write the following in the terminal:
pip install PyMuPDF
This will automatically install fitz and you can use the function fitz.open()
I have the following file which I would like to read, as you can see its incomplete:
file = 'dir2/file.hdf5'
However, I would like to get the full path of file (*):
'/home/user/git_hub_repo/dir1/dir2/file.hdf5'
However, when I do:
from pathlib import Path
filename = Path('dir2/file.hdf5').resolve()
print(filename)
I get:
'/home/user/git_hub_repo/dir2/file.hdf5'
Which is wrong because a dir1 is missing in the retrieved path, how can I get (*) path
Note, that in my terminal i am in:
/home/user/git_hub_repo/
If your current directory is
/home/user/git_hub_repo/
and your file is in
/home/user/git_hub_repo/dir1/dir2/file.hdf5
You should change this
file = 'dir2/file.hdf5'
to
file = 'dir1/dir2/file.hdf5'
I am using python selenium to get a screenshot of test, but in ubuntu the saved screenshot is taking the path name as file name and getting saved on the desktop. I have used the same code on windows and the file was being saved in the correct destination:
def shot():
ts = time.time()
path = "\home\sudhanshu\Desktop\shots\sb"
extension = ".png"
screensave = datetime.datetime.fromtimestamp(ts).strftime('%d%m%Y%H%M%S')
print (path+screensave+extension)
wd.save_screenshot(path+screensave+extension)
Here, if you see the path, I want to save the file with timestamp as file name in a folder named shots present on desktop, but it takes the complete path as file name and gets saved on the desktop. The same thing worked perfectly on windows. I have tried adding the path differently like setting ~\sudhanshu\Desktop\shots06062017170730.png as path but nothing works. Can anyone please suggest something.
You should use os.path.join and os.filesep to manipulate filepaths.
import os
import time
def shot():
ts = time.time()
path = os.path.join("home","sudhanshu","Desktop","shots","sb")
extention = ".png"
screensave = datetime.datetime.fromtimestamp(ts).strftime('%d%m%Y%H%M%S')
print(os.path.join(path, screensave+extention))
wd.save_screenshot(os.path.join(path, screensave+extention))
Started working again by using / and using os.path.join together. Thanks everyone.
I would like to use images with tkinter's canvas but I can't open image with Pillow. In fact, I have all my images in the same folder as my code but when I put "icon.png" it doesn't work. Then when I put the full path to the image (C:/Users/myName/Desktop/PythonWork/game/src/icon.png) , it works.
File "F:\Python\lib\site-packages\PIL\Image.py", line 2312, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'icon.png'
Therefore my question is : How do I make the relative path to work ?
Thanks in advance for your help :)
If you want to reference files in the same directory as in Image.py, put this in Image.py:
import os
# get the directory path of the current python file
my_path = os.path.dirname(__file__)
You can then append to that path the name of the file you want to access.
I am trying to use the listdir function from the os module in python to recover a list of filenames from a particular folder.
here's the code:
import os
def rename_file():
# extract filenames from a folder
#for each filename, rename filename
list_of_files = os.listdir("/home/admin-pc/Downloads/prank/prank")
print (list_of_files)
I am getting the following error:
OSError: [Errno 2] No such file or directory:
it seems to give no trouble in windows, where you start your directory structure from the c drive.
how do i modify the code to work in linux?
The code is correct. There should be some error with the path you provided.
You could open a terminal and enter into the folder first. In the terminal, just key in pwd, then you could get the correct path.
Hope that works.
You could modify your function to exclude that error with check of existence of file/directory:
import os
def rename_file():
# extract filenames from a folder
#for each filename, rename filename
path_to_file = "/home/admin-pc/Downloads/prank/prank"
if os.exists(path_to_file):
list_of_files = os.listdir(path_to_file)
print (list_of_files)