I want to convert pdf to images.
I am using pdf2jpg Module for this.
But It is giving me Following Error:
[WinError 2] The system cannot find the file specified
The path Which I am giving, is correct.
This is my code:
import os
from pdf2jpg import pdf2jpg
find=os.getcwd()
i_p=r"find/k.pdf"
o_p=r"find/data"
result = pdf2jpg.convert_pdf2jpg(i_p, o_p, pages="ALL")
You are setting the input and output paths incorrectly. Assuming your input PDF is in the same directory as your script, following code should work:
i_p = os.path.join(find, "k.pdf")
o_p = os.path.join(find, "data")
Related
I´m running my python file in a directory, let´s say: /python/python_file.py
and the json relative is: /python/python_apps_file/disctionary.json
and all I need to do is:
import json
import random
import os.path
with open(".\python_apps_file\english_dictionary.json","r") as dictionary:
my_dictionary = list(json.load(dictionary).keys())
print(dictionary[5])
I even tried with:
with open(os.path.abspath("english_dictionary.json"),"r") as dictionary:
my_dictionary = list(json.load(dictionary).keys())
but still getting the same error: FileNotFoundError: [Errno 2] No such file or directory:
In your example the filenames are different. It might be a spelling problem.
I'm a Mac user. I'm trying to load a .gpx file on python,using the following code:
import gpxpy
import gpxpy.gpx
gpx_file = open('Downloads/UAQjXL9WRKY.gpx', 'r')
And I get the following message:
FileNotFoundError: [Errno 2] No such file or directory: 'Downloads/UAQjXL9WRKY.gpx'
Could someone help me figure out why? Thanks in advance.
Obviously, one reason would be that the file does not, in fact, exist, but let us assume that it does.
A relative filename (i.e, one that does not start with a /) is interpreted relative to the current working direcory of the process. You are apparently expecting that to be the user's home directory, and you are apparently wrong.
One way around this would be to explicitly add the user's home directory to the filename:
import os
home = os.path.expanduser('~')
absfn = os.path.join(home, 'Downloads/whatever.gpx')
gpx_file = open(absfn, ...)
I have to create a empty text file called file.txt under this path
/home/project/test*/today/file.txt
test* changes each time like test_product or test_1 etc..
I tried the following code:
if(os.path.exists("/home/project/test*/today/file.txt"):
print "Found"
else:
open("/home/project/test*/today/file.txt",a).close()```
I got this error
```IOError: [Errno 2] No such file or directory: '/home/project/test*/today/file.txt'```
I understand I can use glob to search the files with paths like * , but I am unable to figure out how to create a file while having * in the path.
Your code logically doesn't make any sense. What you're basically saying is
if the file exists: display the text Found
otherwise the file does not exist, so try to open the file that does not exist
I'll tell you why it doesn't work. os module does not support wildcards.
If you wanted to use wildcards * you could use glob.
import glob
import os
from pathlib import Path
def check_for_files(filepath):
for filepath_object in glob.glob(filepath):
if os.path.isfile(filepath_object):
return True
return False
file_to_check = "/home/project/test*/today/file.txt"
if not (check_for_files(file_to_check):
Path(file_to_check).touch()
I am trying to open the image from this directory but am not been able to.
It gives me the following error:
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect.
This is my code:
import os, random
random.choice(os.listdir("C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"+'png'))
In here , in the folder backgrounds there are many images.I want a random Image to open.But while running the program , I am getting WindowsError.
What am I doing wrong?
Edit 1
I tried this:
random.choice(os.listdir(r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"+'png'))
But am getting the error:
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect:
'C:\Users\rkp10\AppData\Local\Google\Chrome\User
Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds\*.png/.'
Edit 2
I tried this:
import os, random
a=random.choice(os.listdir(r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"))
os.open(a)
Now I dont get error but it does not open the image either.
Edit 3
I have also tried:
import random,os
folder= "C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"
a=random.choice(os.listdir(folder))
print(a)
from PIL import Image
file = folder+'\\'+a
Image.open(file).show()
#os.open(a, os.O_RDWR)
from PIL import Image
file = folder+'\\'+a
Image.open(file).show()
But got the following error once again:
Traceback (most recent call last): File "G:\Grade 12
Project\auto.py", line 4, in
a=random.choice(os.listdir(folder)) WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:
'C:\Users\rkp10\AppData\Local\Google\Chrome\User
Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca'
Here(the yellow highlighted part) is the directory where my images are stored.
Use the PIL instead.
import os, random
folder=r"D:\Study\SO"
a=random.choice(os.listdir(folder))
print(a)
#os.open(a, os.O_RDWR)
from PIL import Image
file = folder+'\\'+a
Image.open(file).show()
source:
Open and display .png file in python using PIL
The problem with this is that a doesn't have absolute path to that chosen random files.
In Edit 1, the "png" gets concatenated but there is no folder named "backgroundspng"
In Edit 2, you haven't given the flags for os.open() which can be found here.
In Edit 3, please make sure that you are using the r before string.
In you case, use this :
folder = r"C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"
You need to use a raw string by prepending the string with r:
random.choice(os.listdir(r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"+'png'))
Else, you would need to escape each backslash by another backslash.
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)