3
import os
import rarfile

file = input("Password List Directory: ")
rarFile = input("Rar File: ")

passwordList = open(os.path.dirname(file+'.txt'),"r")

with this code I am getting the error:

Traceback (most recent call last):
  File "C:\Users\Nick     L\Desktop\Programming\PythonProgramming\RarCracker.py", line 7, in <module>
    passwordList = open(os.path.dirname(file+'.txt'),"r")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Nick L\\Desktop'

This is weird because I have full permission to this file as I can edit it and do whatever I want, and I am only trying to read it. Every other question I read on stackoverflow was regarding writing to a file and getting a permissions error.

OntologicalSin
  • 144
  • 2
  • 4
  • 15

2 Answers2

7

You're trying to open a directory, not a file, because of the call to dirname on this line:

passwordList = open(os.path.dirname(file+'.txt'),"r")

To open the file instead of the directory containing it, you want something like:

passwordList = open(file + '.txt', 'r')

Or better yet, use the with construct to guarantee that the file is closed after you're done with it.

with open(file + '.txt', 'r') as passwordList:
    # Use passwordList here.
    ...

# passwordList has now been closed for you.

On Linux, trying to open a directory raises an IsADirectoryError in Python 3.5, and an IOError in Python 3.1:

IsADirectoryError: [Errno 21] Is a directory: '/home/kjc/'

I don't have a Windows box to test this on, but according to Daoctor's comment, at least one version of Windows raises a PermissionError when you try to open a directory.

PS: I think you should either trust the user to enter the whole directory-and-file name him- or herself --- without you appending the '.txt' to it --- or you should ask for just the directory, and then append a default filename to it (like os.path.join(directory, 'passwords.txt')).

Either way, asking for a "directory" and then storing it in a variable named file is guaranteed to be confusing, so pick one or the other.

Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
  • 1
    The Windows API error is `ERROR_ACCESS_DENIED` when you try to open a directory as a regular file (as opposed to as a directory). It's not a particularly helpful error, especially considering that the source error coming from the NT kernel is the much more helpful `STATUS_FILE_IS_A_DIRECTORY`. Unfortunately the Windows API is from ancient times and probably trying to remain compatible with Win16 running on MS-DOS. – Eryk Sun Aug 02 '16 at 05:23
  • @eryksun Thanks for the background... I was wondering why the error message was so misleading. – Kevin J. Chase Aug 02 '16 at 05:41
2

os.path.dirname() will return the Directory in which the file is present not the file path. For example if file.txt is in path= 'C:/Users/Desktop/file.txt' then os.path.dirname(path)wil return 'C:/Users/Desktop' as output, while the open() function expects a file path. You can change the current working directory to file location and open the file directly.

os.chdir(<File Directory>)
open(<filename>,'r')

or

open(os.path.join(<fileDirectory>,<fileName>),'r')
Ankur
  • 185
  • 1
  • 2
  • 10