def test_sane_template(self):
# gettempprefix returns a nonempty prefix string
p = tempfile.gettempprefix()
self.assertIsInstance(p, str)
self.assertTrue(len(p) > 0)
python类gettempprefix()的实例源码
def test_usable_template(self):
# gettempprefix returns a usable prefix string
# Create a temp directory, avoiding use of the prefix.
# Then attempt to create a file whose name is
# prefix + 'xxxxxx.xxx' in that directory.
p = tempfile.gettempprefix() + "xxxxxx.xxx"
d = tempfile.mkdtemp(prefix="")
try:
p = os.path.join(d, p)
fd = os.open(p, os.O_RDWR | os.O_CREAT)
os.close(fd)
os.unlink(p)
finally:
os.rmdir(d)
def test_sane_template(self):
# gettempprefix returns a nonempty prefix string
p = tempfile.gettempprefix()
self.assertIsInstance(p, basestring)
self.assertTrue(len(p) > 0)
def test_sane_template(self):
# gettempprefix returns a nonempty prefix string
p = tempfile.gettempprefix()
self.assertIsInstance(p, str)
self.assertTrue(len(p) > 0)
def test_usable_template(self):
# gettempprefix returns a usable prefix string
# Create a temp directory, avoiding use of the prefix.
# Then attempt to create a file whose name is
# prefix + 'xxxxxx.xxx' in that directory.
p = tempfile.gettempprefix() + "xxxxxx.xxx"
d = tempfile.mkdtemp(prefix="")
try:
p = os.path.join(d, p)
fd = os.open(p, os.O_RDWR | os.O_CREAT)
os.close(fd)
os.unlink(p)
finally:
os.rmdir(d)
def NamedTemporaryFile(mode='w+b', bufsize=-1,
suffix="tmp", prefix=gettempprefix(), dir=None, delete=True):
""" Variation on tempfile.NamedTemporaryFile(…), such that suffixes are passed
WITHOUT specifying the period in front (versus the standard library version
which makes you pass suffixes WITH the fucking period, ugh).
"""
from tempfile import _bin_openflags, _text_openflags, \
_mkstemp_inner, _os, \
_TemporaryFileWrapper, \
gettempdir
if dir is None:
dir = gettempdir()
if 'b' in mode:
flags = _bin_openflags
else:
flags = _text_openflags
if _os.name == 'nt' and delete:
flags |= _os.O_TEMPORARY
(fd, name) = _mkstemp_inner(dir, prefix, ".%s" % suffix, flags)
try:
file = _os.fdopen(fd, mode, bufsize)
return _TemporaryFileWrapper(file, name, delete)
except BaseException as baseexc:
_os.unlink(name)
_os.close(fd)
raise FilesystemError(str(baseexc))
def test_get_as_csv(self):
temp_file = os.path.join(tempfile.gettempdir(),
'%s%s.tmp' % (tempfile.gettempprefix(), datetime.now().strftime('%f')))
with open(temp_file, 'wb') as f:
self.test_client.datasets.get_csv(self.ds_name, f, page_size=1000)
with open(temp_file, 'r') as f:
# the +1 is for the headers written to the file
self.assertEqual(len(self.data) + 1, len(f.readlines()))
os.remove(temp_file)