1919from pandas .io .parsers import (read_csv , read_table , read_fwf ,
2020 TextFileReader , TextParser )
2121from pandas .util .testing import (assert_almost_equal ,
22- assert_series_equal , network )
22+ assert_series_equal ,
23+ network ,
24+ ensure_clean )
2325import pandas .util .testing as tm
2426import pandas as pd
2527
@@ -1372,29 +1374,25 @@ def test_utf16_bom_skiprows(self):
13721374
13731375 path = '__%s__.csv' % tm .rands (10 )
13741376
1375- for sep , dat in [('\t ' , data ), (',' , data2 )]:
1376- for enc in ['utf-16' , 'utf-16le' , 'utf-16be' ]:
1377- bytes = dat .encode (enc )
1378- with open (path , 'wb' ) as f :
1379- f .write (bytes )
1380-
1381- s = BytesIO (dat .encode ('utf-8' ))
1382- if py3compat .PY3 :
1383- # somewhat False since the code never sees bytes
1384- from io import TextIOWrapper
1385- s = TextIOWrapper (s , encoding = 'utf-8' )
1386-
1387- result = self .read_csv (path , encoding = enc , skiprows = 2 ,
1388- sep = sep )
1389- expected = self .read_csv (s , encoding = 'utf-8' , skiprows = 2 ,
1390- sep = sep )
1391-
1392- tm .assert_frame_equal (result , expected )
1393-
1394- try :
1395- os .remove (path )
1396- except os .error :
1397- pass
1377+ with ensure_clean (path ) as path :
1378+ for sep , dat in [('\t ' , data ), (',' , data2 )]:
1379+ for enc in ['utf-16' , 'utf-16le' , 'utf-16be' ]:
1380+ bytes = dat .encode (enc )
1381+ with open (path , 'wb' ) as f :
1382+ f .write (bytes )
1383+
1384+ s = BytesIO (dat .encode ('utf-8' ))
1385+ if py3compat .PY3 :
1386+ # somewhat False since the code never sees bytes
1387+ from io import TextIOWrapper
1388+ s = TextIOWrapper (s , encoding = 'utf-8' )
1389+
1390+ result = self .read_csv (path , encoding = enc , skiprows = 2 ,
1391+ sep = sep )
1392+ expected = self .read_csv (s , encoding = 'utf-8' , skiprows = 2 ,
1393+ sep = sep )
1394+
1395+ tm .assert_frame_equal (result , expected )
13981396
13991397 def test_utf16_example (self ):
14001398 path = os .path .join (self .dirpath , 'utf16_ex.txt' )
@@ -1722,32 +1720,27 @@ def test_iteration_open_handle(self):
17221720 if PY3 :
17231721 raise nose .SkipTest
17241722
1725- with open ('__foo__.txt' , 'wb' ) as f :
1726- f .write ('AAA\n BBB\n CCC\n DDD\n EEE\n FFF\n GGG' )
1723+ with ensure_clean () as path :
1724+ with open (path , 'wb' ) as f :
1725+ f .write ('AAA\n BBB\n CCC\n DDD\n EEE\n FFF\n GGG' )
17271726
1728- with open ('__foo__.txt' , 'rb' ) as f :
1729- for line in f :
1730- if 'CCC' in line :
1731- break
1727+ with open (path , 'rb' ) as f :
1728+ for line in f :
1729+ if 'CCC' in line :
1730+ break
17321731
1733- try :
1734- read_table (f , squeeze = True , header = None , engine = 'c' )
1735- except Exception :
1736- pass
1737- else :
1738- raise ValueError ('this should not happen' )
1739-
1740- result = read_table (f , squeeze = True , header = None ,
1741- engine = 'python' )
1732+ try :
1733+ read_table (f , squeeze = True , header = None , engine = 'c' )
1734+ except Exception :
1735+ pass
1736+ else :
1737+ raise ValueError ('this should not happen' )
17421738
1743- expected = Series (['DDD' , 'EEE' , 'FFF' , 'GGG' ])
1744- tm .assert_series_equal (result , expected )
1745-
1746- try :
1747- os .remove ('__foo__.txt' )
1748- except os .error :
1749- pass
1739+ result = read_table (f , squeeze = True , header = None ,
1740+ engine = 'python' )
17501741
1742+ expected = Series (['DDD' , 'EEE' , 'FFF' , 'GGG' ])
1743+ tm .assert_series_equal (result , expected )
17511744
17521745class TestCParserHighMemory (ParserTests , unittest .TestCase ):
17531746
@@ -1924,41 +1917,30 @@ def test_decompression(self):
19241917 data = open (self .csv1 , 'rb' ).read ()
19251918 expected = self .read_csv (self .csv1 )
19261919
1927- try :
1928- tmp = gzip .GzipFile ('__tmp__' , mode = 'wb' )
1920+ with ensure_clean () as path :
1921+ tmp = gzip .GzipFile (path , mode = 'wb' )
19291922 tmp .write (data )
19301923 tmp .close ()
19311924
1932- result = self .read_csv ('__tmp__' , compression = 'gzip' )
1925+ result = self .read_csv (path , compression = 'gzip' )
19331926 tm .assert_frame_equal (result , expected )
19341927
1935- result = self .read_csv (open ('__tmp__' , 'rb' ), compression = 'gzip' )
1928+ result = self .read_csv (open (path , 'rb' ), compression = 'gzip' )
19361929 tm .assert_frame_equal (result , expected )
1937- finally :
1938- # try:
1939- # os.remove('__tmp__')
1940- # except:
1941- # pass
1942- pass
19431930
1944- try :
1945- tmp = bz2 .BZ2File ('__tmp__' , mode = 'wb' )
1931+ with ensure_clean () as path :
1932+ tmp = bz2 .BZ2File (path , mode = 'wb' )
19461933 tmp .write (data )
19471934 tmp .close ()
19481935
1949- result = self .read_csv ('__tmp__' , compression = 'bz2' )
1936+ result = self .read_csv (path , compression = 'bz2' )
19501937 tm .assert_frame_equal (result , expected )
19511938
1952- # result = self.read_csv(open('__tmp__' , 'rb'), compression='bz2')
1939+ # result = self.read_csv(open(path , 'rb'), compression='bz2')
19531940 # tm.assert_frame_equal(result, expected)
19541941
19551942 self .assertRaises (ValueError , self .read_csv ,
1956- '__tmp__' , compression = 'bz3' )
1957- finally :
1958- try :
1959- os .remove ('__tmp__' )
1960- except :
1961- pass
1943+ path , compression = 'bz3' )
19621944
19631945 def test_decompression_regex_sep (self ):
19641946 try :
@@ -1971,35 +1953,24 @@ def test_decompression_regex_sep(self):
19711953 data = data .replace (b',' , b'::' )
19721954 expected = self .read_csv (self .csv1 )
19731955
1974- try :
1975- tmp = gzip .GzipFile ('__tmp__' , mode = 'wb' )
1956+ with ensure_clean () as path :
1957+ tmp = gzip .GzipFile (path , mode = 'wb' )
19761958 tmp .write (data )
19771959 tmp .close ()
19781960
1979- result = self .read_csv ('__tmp__' , sep = '::' , compression = 'gzip' )
1961+ result = self .read_csv (path , sep = '::' , compression = 'gzip' )
19801962 tm .assert_frame_equal (result , expected )
1981- finally :
1982- # try:
1983- # os.remove('__tmp__')
1984- # except:
1985- # pass
1986- pass
19871963
1988- try :
1989- tmp = bz2 .BZ2File ('__tmp__' , mode = 'wb' )
1964+ with ensure_clean () as path :
1965+ tmp = bz2 .BZ2File (path , mode = 'wb' )
19901966 tmp .write (data )
19911967 tmp .close ()
19921968
1993- result = self .read_csv ('__tmp__' , sep = '::' , compression = 'bz2' )
1969+ result = self .read_csv (path , sep = '::' , compression = 'bz2' )
19941970 tm .assert_frame_equal (result , expected )
19951971
19961972 self .assertRaises (ValueError , self .read_csv ,
1997- '__tmp__' , compression = 'bz3' )
1998- finally :
1999- try :
2000- os .remove ('__tmp__' )
2001- except :
2002- pass
1973+ path , compression = 'bz3' )
20031974
20041975 def test_memory_map (self ):
20051976 # it works!
0 commit comments