Kapil Sharma
Kapil Sharma

Reputation: 1

Can't encrypt inner most file

Folder structure:

                       Folder
                        /  \
                       /    \
                subfolder1   files
                    /\         
                   /  \         
    inner_subfolder1   files
                /\
               /  \
sub_inner_folder   files
            /
        files

Problem here is files in sub_inner_folder are not encrypted.

def encypt_Files():
    for folder, subfolders, files in os.walk('/home/username/Desktop/folder'):

        for subfolder in subfolders:
            
            os.chdir(folder)
            
            for files in os.listdir():
                
                if files.endswith('.pdf'):
                    
                    PdfReaderobj = PyPDF2.PdfFileReader(open(files, 'rb'))
                    PdfWriterobj = PyPDF2.PdfFileWriter()
                    if PdfReaderobj.isEncrypted:
                        break
                    else:
                        PdfWriterobj.addPage(PdfReaderobj.getPage(0))
                        PdfWriterobj.encrypt(sys.argv[1])
                        
                        resultPdf = open(files.strip('.pdf')+'_encrypted.pdf', 'wb')
                        PdfWriterobj.write(resultPdf)
                        resultPdf.close()

Upvotes: 0

Views: 72

Answers (3)

Kapil Sharma
Kapil Sharma

Reputation: 1

foldername=[] # used to store folder paths

for folders,subfolders,files in os.walk(path): foldername.append(folders) # storing folder paths in list

for file_path in foldername:

os.chdir(file_path)  # changing directory path from stored folder paths

for files in os.listdir():
    
    if files.endswith('.pdf'):
        pdfReaderobj=PyPDF2.PdfFileReader(open(files,'rb'))
        pdfWriterobj=PyPDF2.PdfFileWriter()
        
        
        if pdfReaderobj.isEncrypted:
            continue
        else:
            pdfWriterobj.addPage(pdfReaderobj.getPage(0))
            pdfWriterobj.encrypt(sys.argv[1])
            
            resultPdf=open(files.strip('.pdf')+'_encrypted.pdf','wb')
            pdfWriterobj.write(resultPdf)
            resultPdf.close()

Upvotes: 0

jcomeau_ictx
jcomeau_ictx

Reputation: 38432

One problem I see is that you're breaking out of the inner for loop on finding an encrypted file. That should probably be a continue, but your making a new iterator using files suggests you may need to rethink the whole strategy.

And another problem is that you're chdiring to a relative path that may no longer be relative to where you are. I suggest using os.path.join instead.

Oh, and you're chdiring to folder instead of to the presumably intended subfolder.

I suggest you start over. Use the files iterator provided by os.walk, and use os.path.join to list out the full path to each file in the directory structure. Then add your pdf encryption code using the full path to each file, and ditch chdir.

Upvotes: 0

Thomas Weller
Thomas Weller

Reputation: 59305

probem here is files in sub_inner_folder are not encrypted.

You do os.chdir(folder) where is should be os.chdir(subfolder). Also, you need to change the directory back using os.chdir("..") when you're done with that directory.

If you start on the wrong working directory, you won't be able to chdir() anywhere. So you need a os.chdir("/home/username/Desktop/folder") first.

Also, permission may break out of the loop. Add

except FileNotFoundError:
    pass  # or whatever
except PermissionError:
    pass  # or whatever

But: os.walk() already gives you a list of files. You should just need to loop over these. That way you also get rid of os.listdir()

Yet another option which sounds totally reasonable to me:

import glob
for result in glob.iglob('/home/username/Desktop/folder/**/*.pdf'):
    print(result)

Upvotes: 1

Related Questions