diff --git a/mercurial/util.py b/mercurial/util.py
index a3b182dd548aa13ca9cc24eb6f364a3e0a1f8893_bWVyY3VyaWFsL3V0aWwucHk=..551aa6e279291c59df5e0c39351962c1e30a0087_bWVyY3VyaWFsL3V0aWwucHk= 100644
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -863,9 +863,11 @@
                 nlink = nlinks(f)
             except OSError:
                 nlink = 0
-                d = os.path.dirname(f)
-                if not os.path.isdir(d):
-                    makedirs(d, self.createmode)
+                dirname, basename = os.path.split(f)
+                # Avoid calling makedirs when the path points to a
+                # directory -- the open will raise IOError below.
+                if basename and not os.path.isdir(dirname):
+                    makedirs(dirname, self.createmode)
             if atomictemp:
                 return atomictempfile(f, mode, self.createmode)
             if nlink > 1: