Hey people,
I hava a lil script that moves pdf-files because of the extracted sign. This script shall do this proccess for each file. But it ends after copying the first file with a permission error, because this file is already in use.
I guess its because the PyPDF4-module opens the file. But I am not shure how to close the file the right way, so my loop-function can do the rest. I am very unexperienced with python. Maybe there is simple way i dont get.
Here“s my code:
If I do it like closing the file, I get the error, that the path already exists. But it moves the file so far. But because of the error the process ends again, so only one file is moved, but not all the other.
Ok I solved the issue by using the copy-method, not the move-method.
I hava a lil script that moves pdf-files because of the extracted sign. This script shall do this proccess for each file. But it ends after copying the first file with a permission error, because this file is already in use.
I guess its because the PyPDF4-module opens the file. But I am not shure how to close the file the right way, so my loop-function can do the rest. I am very unexperienced with python. Maybe there is simple way i dont get.
Here“s my code:
import PyPDF4 import re import io # import mysql.connector import pyodbc # import Organize import os, sys import shutil path = "H:\\Scans\\TestScan\\" dirs = os.listdir( path ) for file in dirs: pdfFileObj = open(f"H:\Scans\TestScan\{file}", 'rb') pdfReader = PyPDF4.PdfFileReader(pdfFileObj) pageObj = pdfReader.getPage(0) pages_text = pageObj.extractText() for line in pages_text.split('\n'): if re.match(r"AN", line): # print(line) prefix, number = line.split('AN', 1) print(number) shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}') #for sign in number: # shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}')
If I do it like closing the file, I get the error, that the path already exists. But it moves the file so far. But because of the error the process ends again, so only one file is moved, but not all the other.
for file in dirs: pdfFileObj = open(f"H:\Scans\TestScan\{file}", 'rb') pdfReader = PyPDF4.PdfFileReader(pdfFileObj) pageObj = pdfReader.getPage(0) pages_text = pageObj.extractText() for line in pages_text.split('\n'): if re.match(r"AN", line): # print(line) prefix, number = line.split('AN', 1) print(number) # shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}') pdfFileObj.close() for sign in number: shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}')
Ok I solved the issue by using the copy-method, not the move-method.
... for line in pages_text.split('\n'): if re.match(r"AN", line): # print(line) prefix, number = line.split('AN', 1) print(number) # shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}') pdfFileObj.close() for sign in number: shutil.copy(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}')I dont realy like it this unclean way. Adding a next method for deleting moved files would work ofcourse, but it feels like a crappy way around it :D