I have been trying to debug this code for merging a folder of pdf's into one pdf file:
import os
from PyPDF2 import PdfFileMerger
loc = "C:\\Users\\anzal\\desktop\\pdf"
x = [a for a in os.listdir(loc) if a.endswith(".pdf")]
print(x)
merger = PdfFileMerger()
for pdf in x:
merger.append(open(pdf,'rb'))
with open("result.pdf", "wb") as fout:
merger.write(fout)
But it doesn't recognize the pdf files - I get the following error:
['A1098e.pdf', 'J1098e.pdf']
Traceback (most recent call last):
File "combopdf.py", line 14, in <module>
merger.append(open(pdf,'rb'))
FileNotFoundError: [Errno 2] No such file or directory: 'A1098e.pdf'
Any ideas on how to fix this? Thanks.
Use absolute paths:
loc = "C:\\Users\\anzal\\desktop\\pdf"
x = [loc+"\\"+a for a in os.listdir(loc) if a.endswith(".pdf")]
^^^^^^^^
add this
Right now it's looking for the .pdf files in the directory from which the script is being ran, and I'm pretty sure that's not C:/Users/anzal/desktop/pdf.
Related
I have multiple PDF files that have different prefixes. I want to merge these pdf files based on the third prefix (third value in the underscore). I want to do this using python library PyPDF2.
This is the error message
Traceback (most recent call last):
File "C:/test2.py", line 12, in <module>
merger.append(filename)
File "C:\py\lib\site-packages\PyPDF2\merger.py", line 203, in append
self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
File "C:\py\lib\site-packages\PyPDF2\merger.py", line 114, in merge
fileobj = file(fileobj, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '0_2021_564495_12345.pdf'
Process finished with exit code 1
For example:
0_2021_1_123.pdf
0_2021_1_1234.pdf
0_2021_1_12345.pdf
0_2021_2_123.pdf
0_2021_2_1234.pdf
0_2021_2_12345.pdf
Expected outcome
1_merged.pdf
2_merged.pdf
Here is what i tried but i am getting an error and it is not working. Any help is much appreciated.
from PyPDF2 import PdfFileMerger
import io
import os
files = os.listdir("C:\\test\\raw")
x=0
merger = PdfFileMerger()
for filename in files:
print(filename.split('_')[2])
prefix = filename.split('_')[2]
if filename.split('_')[2] == prefix:
merger.append(filename)
merger.write("C:\\test\\result" + prefix + "_merged.pdf")
merger.close()
I have watched a video to learn how to merge PDF files into one PDF file. I tried to modify a little in the code so as to deal with a folder which has the PDF files
The main folder (Spyder) has the Demo.py and this is the code
import os
from PyPDF2 import PdfFileMerger
source_dir = os.getcwd() + './PDF Files'
merger = PdfFileMerger()
for item in os.listdir(source_dir):
if item.endswith('pdf'):
merger.append(item)
merger.write('.PDF Files/Output/Complete.pdf')
merger.close()
I have a subfolder named PDF Files into the main folder Spyder and in this subfolder I put the PDF files and inside the subfolder PDF Files I created a folder named Output.
I got error file not found as for the 1.pdf although when printing the item inside the loop, I got the PDF names.
The Traceback of error
Traceback (most recent call last):
File "demo.py", line 9, in <module>
merger.append(item)
File "C:\Users\Future\AppData\Local\Programs\Python\Python36\lib\site-packages\PyPDF2\merger.py", line 203, in append
self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
File "C:\Users\Future\AppData\Local\Programs\Python\Python36\lib\site-packages\PyPDF2\merger.py", line 114, in merge
fileobj = file(fileobj, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '1.pdf'
I could solve it like that
import os
from PyPDF2 import PdfFileMerger
source_dir = './PDF Files/'
merger = PdfFileMerger()
for item in os.listdir(source_dir):
if item.endswith('pdf'):
#print(item)
merger.append(source_dir + item)
merger.write(source_dir + 'Output/Complete.pdf')
merger.close()
I am trying to open all the '*.json' files in a directory and get some data out of them
import json
import os #os module imported here
import glob
path = 'C:\Saba\Python Workspace'
for filename in os.listdir(path):
if filename.endswith('.json'):
with open(filename,'r') as f:
data = json.load(filename)
print(data['key'],end="*")
for path in data['paths']:
print(path['method'],end="*")
for resources in path['resources']:
print(resources['key'],end="*")
print("\b"+"$")
This is the Error i get :
Traceback (most recent call last):
File "c:/Saba/Python Workspace/folder.py", line 9, in <module>
with open(filename,'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'order-securityTransferOrders-service-v1.0.0-inventory.json'
You are running the script in different path. Adding the absolute path of the filename will do the trick.
import os.path
for filename in os.listdir(path):
abs_file_path = os.path.abspath(filename)
if filename.endswith('.json'):
with open(abs_file_path,'r') as f:
# your code ....
replace line with open(filename,'r') as f: with with open(os.path.abspath(filename),'r') as f:
Where do you run this script ? Python tries to search for your Json file in the current script folder.
If you want Python to find within the given path, you should write somthin like :
with open(os.path.join(path,filename) ,'r') as f:
I am trying to loop over a Python directory, and I have a specific file that happens to be the last file in the directory such that I get an IOerror for that specific file.
The error I get is:
IOError: [Errno 2] No such file or directory: 'nod_gyro_instance_11_P_4.csv'
My script:
for filename in os.listdir("/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro"):
data = []
if filename.endswith(".csv"):
data.append(k_fold(filename))
continue
else:
continue
k_fold does this:
def k_fold(myfile, myseed=11109, k=20):
# Load data
data = open(myfile).readlines()
The entire traceback:
Traceback (most recent call last):
File "/Users/my_name/PycharmProjects/MY_Project/Cross_validation.py", line 30, in <module>
data.append(k_fold(filename))
File "/Users/my_name/PycharmProjects/My_Project/Cross_validation.py", line 8, in k_fold
data = open(myfile).readlines()
IOError: [Errno 2] No such file or directory: 'nod_gyro_instance_11_P_4.csv'
My CSV files are such:
nod_gyro_instance_0_P_4.csv
nod_gyro_instance_0_P_3.csv
nod_gyro_instance_0_P_2.csv
nod_gyro_instance_0_P_5.csv
...
nod_gyro_instance_11_P_4.csv
nod_gyro_instance_10_P_6.csv
nod_gyro_instance_10_P_5.csv
nod_gyro_instance_10_P_4.csv
Why doesn't it recognize my nod_gyro_instance_10_P_4.csv file?
os.listdir returns just filenames, not absolute paths. If you're not currently in that same directory, trying to read the file will fail.
You need to join the dirname onto the filename returned:
data_dir = "/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro"
for filename in os.listdir(data_dir):
k_fold(os.path.join(data_dir, filename))
Alternatively, you could use glob to do both the listing (with full paths) and extension filtering:
import glob
for filename in glob.glob("/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro/*.csv"):
k_fold(filename)
When I try running this script:
from PIL import Image
import os
files = os.listdir('mri')
for file in files:
img = Image.open(file)
I get the following error:
Traceback (most recent call last):
File "resize_image.py", line 6, in <module>
img = Image.open(file)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '6.jpg'
I made sure that 6.jpg is available. And, it seems that I get such error for any image in this location.
How can I fix the issue?
Thanks.
The file names from os.listdir are relative to the directory given. They must be made complete by joining the dirname to their basename.
files = os.listdir('my_folder')
for file in files:
img = Image.open(os.path.join('my_folder', file))
You are getting this error because you haven't add your image in project file folder.
Paste your image in project file and then run the program.
img = Image.open(os.path.join('mri', file))
this worked for me making sure you join the dir with the path
img = Image.open(os.path.join(r'C:\Users\Simin\Desktop\python proj\img.jpg'))
this direct path worked for me