Python Os.mkdir Windows Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Python Try Except Windowserror
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions python mkdir p Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million os.mkdir no such file or directory programmers, just like you, helping each other. Join them; it only takes a minute: Sign up python: why does os.makedirs cause WindowsError? up vote 6 down vote favorite In python, I have made a function to make
Python Windowserror
a directory if does not already exist. def make_directory_if_not_exists(path): try: os.makedirs(path) break except OSError as exception: if exception.errno != errno.EEXIST: raise On Windows, sometimes I will get the following exception: WindowsError: [Error 5] Access is denied: 'C:\\...\\my_path' It seems to happen when the directory is open in the Windows File Browser, but I can't reliably reproduce it. So instead I just made the following workaround. def make_directory_if_not_exists(path): while not os.path.isdir(path): try: os.makedirs(path) break except
Windowserror: [error 5]
OSError as exception: if exception.errno != errno.EEXIST: raise except WindowsError: print "got WindowsError" pass What's going on here, i.e. when does Windows mkdir give such an access error? Is there a better solution? python windows share|improve this question edited Jul 12 '13 at 16:20 asked Jul 12 '13 at 16:00 izak 6341915 1 From docs : Raises an error exception if the leaf directory already exists or cannot be created. –Ashwini Chaudhary Jul 12 '13 at 16:03 @AshwiniChaudhary, I know. I already catch this exception with except OSError. I'm asking about WindowsError. –izak Jul 12 '13 at 16:11 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote accepted A little googling reveals that this error is raised in various different contexts, but most of them have to do with permissions errors. The script may need to be run as administrator, or there may be another program open using one of the directories that you are trying to use. share|improve this answer answered Jul 12 '13 at 16:57 Schickmeister 59628 add a comment| up vote 3 down vote You should use OSError as well as IOError. See this answer, you'll use something like: def make_directory_if_not_exists(path): try: os.makedirs(path) except (IOError, OSError) as exception: if exception.errno != errno.EEXIST: ... share|improve this answer edited N
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company attributeerror: 'module' object has no attribute 'mkdirs' Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Os Makedirs Mode
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just windows error access is denied python like you, helping each other. Join them; it only takes a minute: Sign up Python: Getting a WindowsError instead of an IOError up vote 5 down vote favorite I am trying to understand exceptions with Python 2.7.6, http://stackoverflow.com/questions/17619079/python-why-does-os-makedirs-cause-windowserror on Windows 8. Here's the code I am testing, which aims to create a new directory at My_New_Dir. If the directory already exists, I want to delete the entire directory and its contents, and then create a fresh directory. import os dir = 'My_New_Dir' try: os.mkdir(dir) except IOError as e: print 'exception thrown' shutil.rmtree(dir) os.mkdir(dir) The thing is, the exception is never thrown. The code works fine if the directory does not already exist, http://stackoverflow.com/questions/22027508/python-getting-a-windowserror-instead-of-an-ioerror but if the directory does exist, then I get the error: WindowsError: [Error 183] Cannot create a file when that file already exists: 'My_New_Dir' But according to the Python documentation for os.mkdir(), If the directory already exists, OSError is raised. So why is the Windows error being thrown, rather than the Python exception? python windows exception-handling share|improve this question edited Feb 25 '14 at 22:25 Martijn Pieters♦ 502k7513181473 asked Feb 25 '14 at 22:21 Karnivaurus 2,49042958 kind of relevant: python.org/dev/peps/pep-3151 (clunkyness addressed in Python 3) –mlvljr Jun 7 '15 at 14:47 add a comment| 3 Answers 3 active oldest votes up vote 10 down vote accepted WindowsError is a subclass of OSError. From the exceptions documentation: Raised when a Windows-specific error occurs or when the error number does not correspond to an errno value. The winerror and strerror values are created from the return values of the GetLastError() and FormatMessage() functions from the Windows Platform API. The errno value maps the winerror value to corresponding errno.h values. This is a subclass of OSError. You are trying to catch IOError however, which is not such a parent class of WindowsError; as a result it won't suffice to catch either OSError nor WindowsError. Alter your code to use the correct exception here: try: os.mkdir(dir) except OSError as e: or use WindowsError; th
module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see urandom3, if you want to https://docs.python.org/2/library/os.html manipulate paths, see the urandom2 module, and if you want to read all the lines in all the files on the command line see the urandom1 module. For creating temporary files and directories see the urandom0 module, and for high-level file and directory handling see the /dev/urandom9 module. Notes on the availability of these functions: The design of windows error all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function /dev/urandom8 returns stat information about path in the same format (which happens to have originated with the POSIX interface). Extensions peculiar to a particular operating system are also available python os.mkdir windows through the /dev/urandom7 module, but using them is of course a threat to portability. An "Availability: Unix" note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system. If not separately noted, all functions that claim "Availability: Unix" are supported on Mac OS X, which builds on a Unix core. Note All functions in this module raise /dev/urandom6 in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. exception /dev/urandom5/dev/urandom4¶ An alias for the built-in /dev/urandom3 exception. /dev/urandom2/dev/urandom1¶ The name of the operating system dependent module imported. The following names have currently been registered: /dev/urandom0, CryptGenRandom()9, CryptGenRandom()8, CryptGenRandom()7, CryptGenRandom()6, CryptGenRandom()5. See also CryptGenRandom()4 has a finer granularity. CryptGenRandom()3 gives system-dependent version information. The CryptGenRandom()2 module provides detailed checks for the system's identity. 15.1.1. Process Parameters¶ These fu