Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shutil - permission error
#1
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:

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
Reply
#2
use 'with', it will automatically close the file when done.
import PyPDF4
import re
import io
import pyodbc
import os, sys
import shutil
 
 
path = "H:\\Scans\\TestScan\\"
dirs = os.listdir( path )
 
for file in dirs:
    with open(f"{path}{file}", 'rb') as fp: 
        pdfReader = PyPDF4.PdfFileReader(fp)
        pageObj = pdfReader.getPage(0)
        pages_text = pageObj.extractText()
    
        for line in pages_text.split('\n'):
            if re.match(r"AN", line):
                prefix, number = line.split('AN', 1)
                print(number)

    shutil.move(f'H:\\Scans\\TestScan\\{file}', f'H:\\Akten\\TestAkten\\{number}')
New edit: Modified line 13 and 14 - Can't execute code, so hope OK now
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KivyMD android app - problem with permission polak7gt 0 292 Jan-18-2024, 01:27 PM
Last Post: polak7gt
  use of shutil.copytree with ENOTDIR exception yan 2 912 Nov-29-2023, 03:02 PM
Last Post: yan
  Potential Permission error on Mac OSX Catalina OWOLLC 1 723 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
  logging: change log file permission with RotatingFileHandler erg 0 1,043 Aug-09-2023, 01:24 PM
Last Post: erg
  The INSERT permission was denied on the object Steven5055 2 1,487 Feb-25-2023, 11:37 PM
Last Post: Steven5055
  shutil.move make data corrupt kucingkembar 0 796 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,596 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Permission issue when using scapy jao 3 9,832 Feb-05-2022, 06:14 PM
Last Post: snippsat
  extra slashes in the network path for shutil.copy tester_V 3 3,774 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  Error no 13: Permission denied in python shantanu97 1 6,183 Mar-31-2021, 02:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020