path.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. """ path.py - An object representing a path to a file or directory.
  2. Example:
  3. from path import path
  4. d = path('/home/guido/bin')
  5. for f in d.files('*.py'):
  6. f.chmod(0755)
  7. This module requires Python 2.2 or later.
  8. URL: http://www.jorendorff.com/articles/python/path
  9. Author: Jason Orendorff <jason.orendorff\x40gmail\x2ecom> (and others - see the url!)
  10. Date: 7 Mar 2004
  11. """
  12. # TODO
  13. # - Tree-walking functions don't avoid symlink loops. Matt Harrison sent me a patch for this.
  14. # - Tree-walking functions can't ignore errors. Matt Harrison asked for this.
  15. #
  16. # - Two people asked for path.chdir(). This just seems wrong to me,
  17. # I dunno. chdir() is moderately evil anyway.
  18. #
  19. # - Bug in write_text(). It doesn't support Universal newline mode.
  20. # - Better error message in listdir() when self isn't a
  21. # directory. (On Windows, the error message really sucks.)
  22. # - Make sure everything has a good docstring.
  23. # - Add methods for regex find and replace.
  24. # - guess_content_type() method?
  25. # - Perhaps support arguments to touch().
  26. # - Could add split() and join() methods that generate warnings.
  27. from __future__ import generators
  28. import sys, warnings, os, fnmatch, glob, shutil, codecs, hashlib
  29. __version__ = '2.1'
  30. __all__ = ['path']
  31. # Platform-specific support for path.owner
  32. if os.name == 'nt':
  33. try:
  34. import win32security
  35. except ImportError:
  36. win32security = None
  37. else:
  38. try:
  39. import pwd
  40. except ImportError:
  41. pwd = None
  42. # Pre-2.3 support. Are unicode filenames supported?
  43. _base = str
  44. _getcwd = os.getcwd
  45. try:
  46. if os.path.supports_unicode_filenames:
  47. _base = unicode
  48. _getcwd = os.getcwdu
  49. except AttributeError:
  50. pass
  51. # Pre-2.3 workaround for booleans
  52. try:
  53. True, False
  54. except NameError:
  55. True, False = 1, 0
  56. # Pre-2.3 workaround for basestring.
  57. try:
  58. basestring
  59. except NameError:
  60. basestring = (str, unicode)
  61. # Universal newline support
  62. _textmode = 'r'
  63. if hasattr(file, 'newlines'):
  64. _textmode = 'U'
  65. class TreeWalkWarning(Warning):
  66. pass
  67. class path(_base):
  68. """ Represents a filesystem path.
  69. For documentation on individual methods, consult their
  70. counterparts in os.path.
  71. """
  72. # --- Special Python methods.
  73. def __repr__(self):
  74. return 'path(%s)' % _base.__repr__(_base(self))
  75. # Adding a path and a string yields a path.
  76. def __add__(self, more):
  77. try:
  78. resultStr = _base.__add__(self, more)
  79. except TypeError: #Python bug
  80. resultStr = NotImplemented
  81. if resultStr is NotImplemented:
  82. return resultStr
  83. return self.__class__(resultStr)
  84. def __radd__(self, other):
  85. if isinstance(other, basestring):
  86. return self.__class__(other.__add__(self))
  87. else:
  88. return NotImplemented
  89. # The / operator joins paths.
  90. def __div__(self, rel):
  91. """ fp.__div__(rel) == fp / rel == fp.joinpath(rel)
  92. Join two path components, adding a separator character if
  93. needed.
  94. """
  95. return self.__class__(os.path.join(self, rel))
  96. # Make the / operator work even when true division is enabled.
  97. __truediv__ = __div__
  98. def getcwd(cls):
  99. """ Return the current working directory as a path object. """
  100. return cls(_getcwd())
  101. getcwd = classmethod(getcwd)
  102. # --- Operations on path strings.
  103. isabs = os.path.isabs
  104. def abspath(self): return self.__class__(os.path.abspath(self))
  105. def normcase(self): return self.__class__(os.path.normcase(self))
  106. def normpath(self): return self.__class__(os.path.normpath(self))
  107. def realpath(self): return self.__class__(os.path.realpath(self))
  108. def expanduser(self): return self.__class__(os.path.expanduser(self))
  109. def expandvars(self): return self.__class__(os.path.expandvars(self))
  110. def dirname(self): return self.__class__(os.path.dirname(self))
  111. basename = os.path.basename
  112. def expand(self):
  113. """ Clean up a filename by calling expandvars(),
  114. expanduser(), and normpath() on it.
  115. This is commonly everything needed to clean up a filename
  116. read from a configuration file, for example.
  117. """
  118. return self.expandvars().expanduser().normpath()
  119. def _get_namebase(self):
  120. base, ext = os.path.splitext(self.name)
  121. return base
  122. def _get_ext(self):
  123. f, ext = os.path.splitext(_base(self))
  124. return ext
  125. def _get_drive(self):
  126. drive, r = os.path.splitdrive(self)
  127. return self.__class__(drive)
  128. parent = property(
  129. dirname, None, None,
  130. """ This path's parent directory, as a new path object.
  131. For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')
  132. """)
  133. name = property(
  134. basename, None, None,
  135. """ The name of this file or directory without the full path.
  136. For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'
  137. """)
  138. namebase = property(
  139. _get_namebase, None, None,
  140. """ The same as path.name, but with one file extension stripped off.
  141. For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz',
  142. but path('/home/guido/python.tar.gz').namebase == 'python.tar'
  143. """)
  144. ext = property(
  145. _get_ext, None, None,
  146. """ The file extension, for example '.py'. """)
  147. drive = property(
  148. _get_drive, None, None,
  149. """ The drive specifier, for example 'C:'.
  150. This is always empty on systems that don't use drive specifiers.
  151. """)
  152. def splitpath(self):
  153. """ p.splitpath() -> Return (p.parent, p.name). """
  154. parent, child = os.path.split(self)
  155. return self.__class__(parent), child
  156. def splitdrive(self):
  157. """ p.splitdrive() -> Return (p.drive, <the rest of p>).
  158. Split the drive specifier from this path. If there is
  159. no drive specifier, p.drive is empty, so the return value
  160. is simply (path(''), p). This is always the case on Unix.
  161. """
  162. drive, rel = os.path.splitdrive(self)
  163. return self.__class__(drive), rel
  164. def splitext(self):
  165. """ p.splitext() -> Return (p.stripext(), p.ext).
  166. Split the filename extension from this path and return
  167. the two parts. Either part may be empty.
  168. The extension is everything from '.' to the end of the
  169. last path segment. This has the property that if
  170. (a, b) == p.splitext(), then a + b == p.
  171. """
  172. filename, ext = os.path.splitext(self)
  173. return self.__class__(filename), ext
  174. def stripext(self):
  175. """ p.stripext() -> Remove one file extension from the path.
  176. For example, path('/home/guido/python.tar.gz').stripext()
  177. returns path('/home/guido/python.tar').
  178. """
  179. return self.splitext()[0]
  180. if hasattr(os.path, 'splitunc'):
  181. def splitunc(self):
  182. unc, rest = os.path.splitunc(self)
  183. return self.__class__(unc), rest
  184. def _get_uncshare(self):
  185. unc, r = os.path.splitunc(self)
  186. return self.__class__(unc)
  187. uncshare = property(
  188. _get_uncshare, None, None,
  189. """ The UNC mount point for this path.
  190. This is empty for paths on local drives. """)
  191. def joinpath(self, *args):
  192. """ Join two or more path components, adding a separator
  193. character (os.sep) if needed. Returns a new path
  194. object.
  195. """
  196. return self.__class__(os.path.join(self, *args))
  197. def splitall(self):
  198. r""" Return a list of the path components in this path.
  199. The first item in the list will be a path. Its value will be
  200. either os.curdir, os.pardir, empty, or the root directory of
  201. this path (for example, '/' or 'C:\\'). The other items in
  202. the list will be strings.
  203. path.path.joinpath(*result) will yield the original path.
  204. """
  205. parts = []
  206. loc = self
  207. while loc != os.curdir and loc != os.pardir:
  208. prev = loc
  209. loc, child = prev.splitpath()
  210. if loc == prev:
  211. break
  212. parts.append(child)
  213. parts.append(loc)
  214. parts.reverse()
  215. return parts
  216. def relpath(self):
  217. """ Return this path as a relative path,
  218. based from the current working directory.
  219. """
  220. cwd = self.__class__(os.getcwd())
  221. return cwd.relpathto(self)
  222. def relpathto(self, dest):
  223. """ Return a relative path from self to dest.
  224. If there is no relative path from self to dest, for example if
  225. they reside on different drives in Windows, then this returns
  226. dest.abspath().
  227. """
  228. origin = self.abspath()
  229. dest = self.__class__(dest).abspath()
  230. orig_list = origin.normcase().splitall()
  231. # Don't normcase dest! We want to preserve the case.
  232. dest_list = dest.splitall()
  233. if orig_list[0] != os.path.normcase(dest_list[0]):
  234. # Can't get here from there.
  235. return dest
  236. # Find the location where the two paths start to differ.
  237. i = 0
  238. for start_seg, dest_seg in zip(orig_list, dest_list):
  239. if start_seg != os.path.normcase(dest_seg):
  240. break
  241. i += 1
  242. # Now i is the point where the two paths diverge.
  243. # Need a certain number of "os.pardir"s to work up
  244. # from the origin to the point of divergence.
  245. segments = [os.pardir] * (len(orig_list) - i)
  246. # Need to add the diverging part of dest_list.
  247. segments += dest_list[i:]
  248. if len(segments) == 0:
  249. # If they happen to be identical, use os.curdir.
  250. relpath = os.curdir
  251. else:
  252. relpath = os.path.join(*segments)
  253. return self.__class__(relpath)
  254. # --- Listing, searching, walking, and matching
  255. def listdir(self, pattern=None):
  256. """ D.listdir() -> List of items in this directory.
  257. Use D.files() or D.dirs() instead if you want a listing
  258. of just files or just subdirectories.
  259. The elements of the list are path objects.
  260. With the optional 'pattern' argument, this only lists
  261. items whose names match the given pattern.
  262. """
  263. names = os.listdir(self)
  264. if pattern is not None:
  265. names = fnmatch.filter(names, pattern)
  266. return [self / child for child in names]
  267. def dirs(self, pattern=None):
  268. """ D.dirs() -> List of this directory's subdirectories.
  269. The elements of the list are path objects.
  270. This does not walk recursively into subdirectories
  271. (but see path.walkdirs).
  272. With the optional 'pattern' argument, this only lists
  273. directories whose names match the given pattern. For
  274. example, d.dirs('build-*').
  275. """
  276. return [p for p in self.listdir(pattern) if p.isdir()]
  277. def files(self, pattern=None):
  278. """ D.files() -> List of the files in this directory.
  279. The elements of the list are path objects.
  280. This does not walk into subdirectories (see path.walkfiles).
  281. With the optional 'pattern' argument, this only lists files
  282. whose names match the given pattern. For example,
  283. d.files('*.pyc').
  284. """
  285. return [p for p in self.listdir(pattern) if p.isfile()]
  286. def walk(self, pattern=None, errors='strict'):
  287. """ D.walk() -> iterator over files and subdirs, recursively.
  288. The iterator yields path objects naming each child item of
  289. this directory and its descendants. This requires that
  290. D.isdir().
  291. This performs a depth-first traversal of the directory tree.
  292. Each directory is returned just before all its children.
  293. The errors= keyword argument controls behavior when an
  294. error occurs. The default is 'strict', which causes an
  295. exception. The other allowed values are 'warn', which
  296. reports the error via warnings.warn(), and 'ignore'.
  297. """
  298. if errors not in ('strict', 'warn', 'ignore'):
  299. raise ValueError("invalid errors parameter")
  300. try:
  301. childList = self.listdir()
  302. except Exception:
  303. if errors == 'ignore':
  304. return
  305. elif errors == 'warn':
  306. warnings.warn(
  307. "Unable to list directory '%s': %s"
  308. % (self, sys.exc_info()[1]),
  309. TreeWalkWarning)
  310. else:
  311. raise
  312. for child in childList:
  313. if pattern is None or child.fnmatch(pattern):
  314. yield child
  315. try:
  316. isdir = child.isdir()
  317. except Exception:
  318. if errors == 'ignore':
  319. isdir = False
  320. elif errors == 'warn':
  321. warnings.warn(
  322. "Unable to access '%s': %s"
  323. % (child, sys.exc_info()[1]),
  324. TreeWalkWarning)
  325. isdir = False
  326. else:
  327. raise
  328. if isdir:
  329. for item in child.walk(pattern, errors):
  330. yield item
  331. def walkdirs(self, pattern=None, errors='strict'):
  332. """ D.walkdirs() -> iterator over subdirs, recursively.
  333. With the optional 'pattern' argument, this yields only
  334. directories whose names match the given pattern. For
  335. example, mydir.walkdirs('*test') yields only directories
  336. with names ending in 'test'.
  337. The errors= keyword argument controls behavior when an
  338. error occurs. The default is 'strict', which causes an
  339. exception. The other allowed values are 'warn', which
  340. reports the error via warnings.warn(), and 'ignore'.
  341. """
  342. if errors not in ('strict', 'warn', 'ignore'):
  343. raise ValueError("invalid errors parameter")
  344. try:
  345. dirs = self.dirs()
  346. except Exception:
  347. if errors == 'ignore':
  348. return
  349. elif errors == 'warn':
  350. warnings.warn(
  351. "Unable to list directory '%s': %s"
  352. % (self, sys.exc_info()[1]),
  353. TreeWalkWarning)
  354. else:
  355. raise
  356. for child in dirs:
  357. if pattern is None or child.fnmatch(pattern):
  358. yield child
  359. for subsubdir in child.walkdirs(pattern, errors):
  360. yield subsubdir
  361. def walkfiles(self, pattern=None, errors='strict'):
  362. """ D.walkfiles() -> iterator over files in D, recursively.
  363. The optional argument, pattern, limits the results to files
  364. with names that match the pattern. For example,
  365. mydir.walkfiles('*.tmp') yields only files with the .tmp
  366. extension.
  367. """
  368. if errors not in ('strict', 'warn', 'ignore'):
  369. raise ValueError("invalid errors parameter")
  370. try:
  371. childList = self.listdir()
  372. except Exception:
  373. if errors == 'ignore':
  374. return
  375. elif errors == 'warn':
  376. warnings.warn(
  377. "Unable to list directory '%s': %s"
  378. % (self, sys.exc_info()[1]),
  379. TreeWalkWarning)
  380. else:
  381. raise
  382. for child in childList:
  383. try:
  384. isfile = child.isfile()
  385. isdir = not isfile and child.isdir()
  386. except:
  387. if errors == 'ignore':
  388. return
  389. elif errors == 'warn':
  390. warnings.warn(
  391. "Unable to access '%s': %s"
  392. % (self, sys.exc_info()[1]),
  393. TreeWalkWarning)
  394. else:
  395. raise
  396. if isfile:
  397. if pattern is None or child.fnmatch(pattern):
  398. yield child
  399. elif isdir:
  400. for f in child.walkfiles(pattern, errors):
  401. yield f
  402. def fnmatch(self, pattern):
  403. """ Return True if self.name matches the given pattern.
  404. pattern - A filename pattern with wildcards,
  405. for example '*.py'.
  406. """
  407. return fnmatch.fnmatch(self.name, pattern)
  408. def glob(self, pattern):
  409. """ Return a list of path objects that match the pattern.
  410. pattern - a path relative to this directory, with wildcards.
  411. For example, path('/users').glob('*/bin/*') returns a list
  412. of all the files users have in their bin directories.
  413. """
  414. cls = self.__class__
  415. return [cls(s) for s in glob.glob(_base(self / pattern))]
  416. # --- Reading or writing an entire file at once.
  417. def open(self, mode='r'):
  418. """ Open this file. Return a file object. """
  419. return file(self, mode)
  420. def bytes(self):
  421. """ Open this file, read all bytes, return them as a string. """
  422. f = self.open('rb')
  423. try:
  424. return f.read()
  425. finally:
  426. f.close()
  427. def write_bytes(self, bytes, append=False):
  428. """ Open this file and write the given bytes to it.
  429. Default behavior is to overwrite any existing file.
  430. Call p.write_bytes(bytes, append=True) to append instead.
  431. """
  432. if append:
  433. mode = 'ab'
  434. else:
  435. mode = 'wb'
  436. f = self.open(mode)
  437. try:
  438. f.write(bytes)
  439. finally:
  440. f.close()
  441. def text(self, encoding=None, errors='strict'):
  442. r""" Open this file, read it in, return the content as a string.
  443. This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r'
  444. are automatically translated to '\n'.
  445. Optional arguments:
  446. encoding - The Unicode encoding (or character set) of
  447. the file. If present, the content of the file is
  448. decoded and returned as a unicode object; otherwise
  449. it is returned as an 8-bit str.
  450. errors - How to handle Unicode errors; see help(str.decode)
  451. for the options. Default is 'strict'.
  452. """
  453. if encoding is None:
  454. # 8-bit
  455. f = self.open(_textmode)
  456. try:
  457. return f.read()
  458. finally:
  459. f.close()
  460. else:
  461. # Unicode
  462. f = codecs.open(self, 'r', encoding, errors)
  463. # (Note - Can't use 'U' mode here, since codecs.open
  464. # doesn't support 'U' mode, even in Python 2.3.)
  465. try:
  466. t = f.read()
  467. finally:
  468. f.close()
  469. return (t.replace(u'\r\n', u'\n')
  470. .replace(u'\r\x85', u'\n')
  471. .replace(u'\r', u'\n')
  472. .replace(u'\x85', u'\n')
  473. .replace(u'\u2028', u'\n'))
  474. def write_text(self, text, encoding=None, errors='strict', linesep=os.linesep, append=False):
  475. r""" Write the given text to this file.
  476. The default behavior is to overwrite any existing file;
  477. to append instead, use the 'append=True' keyword argument.
  478. There are two differences between path.write_text() and
  479. path.write_bytes(): newline handling and Unicode handling.
  480. See below.
  481. Parameters:
  482. - text - str/unicode - The text to be written.
  483. - encoding - str - The Unicode encoding that will be used.
  484. This is ignored if 'text' isn't a Unicode string.
  485. - errors - str - How to handle Unicode encoding errors.
  486. Default is 'strict'. See help(unicode.encode) for the
  487. options. This is ignored if 'text' isn't a Unicode
  488. string.
  489. - linesep - keyword argument - str/unicode - The sequence of
  490. characters to be used to mark end-of-line. The default is
  491. os.linesep. You can also specify None; this means to
  492. leave all newlines as they are in 'text'.
  493. - append - keyword argument - bool - Specifies what to do if
  494. the file already exists (True: append to the end of it;
  495. False: overwrite it.) The default is False.
  496. --- Newline handling.
  497. write_text() converts all standard end-of-line sequences
  498. ('\n', '\r', and '\r\n') to your platform's default end-of-line
  499. sequence (see os.linesep; on Windows, for example, the
  500. end-of-line marker is '\r\n').
  501. If you don't like your platform's default, you can override it
  502. using the 'linesep=' keyword argument. If you specifically want
  503. write_text() to preserve the newlines as-is, use 'linesep=None'.
  504. This applies to Unicode text the same as to 8-bit text, except
  505. there are three additional standard Unicode end-of-line sequences:
  506. u'\x85', u'\r\x85', and u'\u2028'.
  507. (This is slightly different from when you open a file for
  508. writing with fopen(filename, "w") in C or file(filename, 'w')
  509. in Python.)
  510. --- Unicode
  511. If 'text' isn't Unicode, then apart from newline handling, the
  512. bytes are written verbatim to the file. The 'encoding' and
  513. 'errors' arguments are not used and must be omitted.
  514. If 'text' is Unicode, it is first converted to bytes using the
  515. specified 'encoding' (or the default encoding if 'encoding'
  516. isn't specified). The 'errors' argument applies only to this
  517. conversion.
  518. """
  519. if isinstance(text, unicode):
  520. if linesep is not None:
  521. # Convert all standard end-of-line sequences to
  522. # ordinary newline characters.
  523. text = (text.replace(u'\r\n', u'\n')
  524. .replace(u'\r\x85', u'\n')
  525. .replace(u'\r', u'\n')
  526. .replace(u'\x85', u'\n')
  527. .replace(u'\u2028', u'\n'))
  528. text = text.replace(u'\n', linesep)
  529. if encoding is None:
  530. encoding = sys.getdefaultencoding()
  531. bytes = text.encode(encoding, errors)
  532. else:
  533. # It is an error to specify an encoding if 'text' is
  534. # an 8-bit string.
  535. assert encoding is None
  536. if linesep is not None:
  537. text = (text.replace('\r\n', '\n')
  538. .replace('\r', '\n'))
  539. bytes = text.replace('\n', linesep)
  540. self.write_bytes(bytes, append)
  541. def lines(self, encoding=None, errors='strict', retain=True):
  542. r""" Open this file, read all lines, return them in a list.
  543. Optional arguments:
  544. encoding - The Unicode encoding (or character set) of
  545. the file. The default is None, meaning the content
  546. of the file is read as 8-bit characters and returned
  547. as a list of (non-Unicode) str objects.
  548. errors - How to handle Unicode errors; see help(str.decode)
  549. for the options. Default is 'strict'
  550. retain - If true, retain newline characters; but all newline
  551. character combinations ('\r', '\n', '\r\n') are
  552. translated to '\n'. If false, newline characters are
  553. stripped off. Default is True.
  554. This uses 'U' mode in Python 2.3 and later.
  555. """
  556. if encoding is None and retain:
  557. f = self.open(_textmode)
  558. try:
  559. return f.readlines()
  560. finally:
  561. f.close()
  562. else:
  563. return self.text(encoding, errors).splitlines(retain)
  564. def write_lines(self, lines, encoding=None, errors='strict',
  565. linesep=os.linesep, append=False):
  566. r""" Write the given lines of text to this file.
  567. By default this overwrites any existing file at this path.
  568. This puts a platform-specific newline sequence on every line.
  569. See 'linesep' below.
  570. lines - A list of strings.
  571. encoding - A Unicode encoding to use. This applies only if
  572. 'lines' contains any Unicode strings.
  573. errors - How to handle errors in Unicode encoding. This
  574. also applies only to Unicode strings.
  575. linesep - The desired line-ending. This line-ending is
  576. applied to every line. If a line already has any
  577. standard line ending ('\r', '\n', '\r\n', u'\x85',
  578. u'\r\x85', u'\u2028'), that will be stripped off and
  579. this will be used instead. The default is os.linesep,
  580. which is platform-dependent ('\r\n' on Windows, '\n' on
  581. Unix, etc.) Specify None to write the lines as-is,
  582. like file.writelines().
  583. Use the keyword argument append=True to append lines to the
  584. file. The default is to overwrite the file. Warning:
  585. When you use this with Unicode data, if the encoding of the
  586. existing data in the file is different from the encoding
  587. you specify with the encoding= parameter, the result is
  588. mixed-encoding data, which can really confuse someone trying
  589. to read the file later.
  590. """
  591. if append:
  592. mode = 'ab'
  593. else:
  594. mode = 'wb'
  595. f = self.open(mode)
  596. try:
  597. for line in lines:
  598. isUnicode = isinstance(line, unicode)
  599. if linesep is not None:
  600. # Strip off any existing line-end and add the
  601. # specified linesep string.
  602. if isUnicode:
  603. if line[-2:] in (u'\r\n', u'\x0d\x85'):
  604. line = line[:-2]
  605. elif line[-1:] in (u'\r', u'\n',
  606. u'\x85', u'\u2028'):
  607. line = line[:-1]
  608. else:
  609. if line[-2:] == '\r\n':
  610. line = line[:-2]
  611. elif line[-1:] in ('\r', '\n'):
  612. line = line[:-1]
  613. line += linesep
  614. if isUnicode:
  615. if encoding is None:
  616. encoding = sys.getdefaultencoding()
  617. line = line.encode(encoding, errors)
  618. f.write(line)
  619. finally:
  620. f.close()
  621. def read_md5(self):
  622. """ Calculate the md5 hash for this file.
  623. This reads through the entire file.
  624. """
  625. f = self.open('rb')
  626. try:
  627. m = hashlib.md5()
  628. while True:
  629. d = f.read(8192)
  630. if not d:
  631. break
  632. m.update(d)
  633. finally:
  634. f.close()
  635. return m.digest()
  636. # --- Methods for querying the filesystem.
  637. exists = os.path.exists
  638. isdir = os.path.isdir
  639. isfile = os.path.isfile
  640. islink = os.path.islink
  641. ismount = os.path.ismount
  642. if hasattr(os.path, 'samefile'):
  643. samefile = os.path.samefile
  644. getatime = os.path.getatime
  645. atime = property(
  646. getatime, None, None,
  647. """ Last access time of the file. """)
  648. getmtime = os.path.getmtime
  649. mtime = property(
  650. getmtime, None, None,
  651. """ Last-modified time of the file. """)
  652. if hasattr(os.path, 'getctime'):
  653. getctime = os.path.getctime
  654. ctime = property(
  655. getctime, None, None,
  656. """ Creation time of the file. """)
  657. getsize = os.path.getsize
  658. size = property(
  659. getsize, None, None,
  660. """ Size of the file, in bytes. """)
  661. if hasattr(os, 'access'):
  662. def access(self, mode):
  663. """ Return true if current user has access to this path.
  664. mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK
  665. """
  666. return os.access(self, mode)
  667. def stat(self):
  668. """ Perform a stat() system call on this path. """
  669. return os.stat(self)
  670. def lstat(self):
  671. """ Like path.stat(), but do not follow symbolic links. """
  672. return os.lstat(self)
  673. def get_owner(self):
  674. r""" Return the name of the owner of this file or directory.
  675. This follows symbolic links.
  676. On Windows, this returns a name of the form ur'DOMAIN\User Name'.
  677. On Windows, a group can own a file or directory.
  678. """
  679. if os.name == 'nt':
  680. if win32security is None:
  681. raise Exception("path.owner requires win32all to be installed")
  682. desc = win32security.GetFileSecurity(
  683. self, win32security.OWNER_SECURITY_INFORMATION)
  684. sid = desc.GetSecurityDescriptorOwner()
  685. account, domain, typecode = win32security.LookupAccountSid(None, sid)
  686. return domain + u'\\' + account
  687. else:
  688. if pwd is None:
  689. raise NotImplementedError("path.owner is not implemented on this platform.")
  690. st = self.stat()
  691. return pwd.getpwuid(st.st_uid).pw_name
  692. owner = property(
  693. get_owner, None, None,
  694. """ Name of the owner of this file or directory. """)
  695. if hasattr(os, 'statvfs'):
  696. def statvfs(self):
  697. """ Perform a statvfs() system call on this path. """
  698. return os.statvfs(self)
  699. if hasattr(os, 'pathconf'):
  700. def pathconf(self, name):
  701. return os.pathconf(self, name)
  702. # --- Modifying operations on files and directories
  703. def utime(self, times):
  704. """ Set the access and modified times of this file. """
  705. os.utime(self, times)
  706. def chmod(self, mode):
  707. os.chmod(self, mode)
  708. if hasattr(os, 'chown'):
  709. def chown(self, uid, gid):
  710. os.chown(self, uid, gid)
  711. def rename(self, new):
  712. os.rename(self, new)
  713. def renames(self, new):
  714. os.renames(self, new)
  715. # --- Create/delete operations on directories
  716. def mkdir(self, mode=0777):
  717. os.mkdir(self, mode)
  718. def makedirs(self, mode=0777):
  719. os.makedirs(self, mode)
  720. def rmdir(self):
  721. os.rmdir(self)
  722. def removedirs(self):
  723. os.removedirs(self)
  724. # --- Modifying operations on files
  725. def touch(self):
  726. """ Set the access/modified times of this file to the current time.
  727. Create the file if it does not exist.
  728. """
  729. fd = os.open(self, os.O_WRONLY | os.O_CREAT, 0666)
  730. os.close(fd)
  731. os.utime(self, None)
  732. def remove(self):
  733. os.remove(self)
  734. def unlink(self):
  735. os.unlink(self)
  736. # --- Links
  737. if hasattr(os, 'link'):
  738. def link(self, newpath):
  739. """ Create a hard link at 'newpath', pointing to this file. """
  740. os.link(self, newpath)
  741. if hasattr(os, 'symlink'):
  742. def symlink(self, newlink):
  743. """ Create a symbolic link at 'newlink', pointing here. """
  744. os.symlink(self, newlink)
  745. if hasattr(os, 'readlink'):
  746. def readlink(self):
  747. """ Return the path to which this symbolic link points.
  748. The result may be an absolute or a relative path.
  749. """
  750. return self.__class__(os.readlink(self))
  751. def readlinkabs(self):
  752. """ Return the path to which this symbolic link points.
  753. The result is always an absolute path.
  754. """
  755. p = self.readlink()
  756. if p.isabs():
  757. return p
  758. else:
  759. return (self.parent / p).abspath()
  760. # --- High-level functions from shutil
  761. copyfile = shutil.copyfile
  762. copymode = shutil.copymode
  763. copystat = shutil.copystat
  764. copy = shutil.copy
  765. copy2 = shutil.copy2
  766. copytree = shutil.copytree
  767. if hasattr(shutil, 'move'):
  768. move = shutil.move
  769. rmtree = shutil.rmtree
  770. # --- Special stuff from os
  771. if hasattr(os, 'chroot'):
  772. def chroot(self):
  773. os.chroot(self)
  774. if hasattr(os, 'startfile'):
  775. def startfile(self):
  776. os.startfile(self)