如何在两个不同的函数中引用相同的临时目录。我需要访问在 move_source_to_dest 中解压缩的文件,作为 pd.read_csv 语句中函数 df_to_csv 的输入。我尝试了一些更改,但没有任何效果。请帮忙。
def move_source_to_dest(key, src_session):
with tempfile.TemporaryDirectory() as tempdir:
try:
print("downloading {}/{}".format(s3_src_bucket, key))
src_session.client('s3').download_file(Bucket=s3_src_bucket, Key=key,
Filename=os.path.join(tempdir, os.path.basename(key)))
#Command to decompress the files
command = "bzip2 -dk " + os.path.join(tempdir, os.path.basename(key))
subprocess.call(command,shell = True)
except Exception as e:
print("exception handling {}/{}".format(s3_src_bucket, key))
raise e
def df_to_csv(key, src_session):
with tempfile.TemporaryDirectory() as tempdir:
try:
#Reading all the columns names from the file "ambs_ambi_ColumnsNames.txt"
with open('./shakenbake_ds/ambs_ambi_ColumnsNames.txt') as f:
clist= f.read().splitlines()
#file = open('ambs_ambi_ColumnsNames.txt','r')
#clist=file.readlines()
Filename=os.path.join(tempdir, os.path.basename(key[:-4]))
Fileout=os.path.join(tempdir, os.path.basename(key[:-4])) + "-out.csv"
with open('./shakenbake_ds/ambs_ambi_OutColumnsNames.txt') as o:
outcols= o.read().splitlines()
#file = open('ambs_ambi_OutColumnsNames.txt','r')
#outcols=file.readlines()
#global Filename
c=0
for chunk in pd.read_csv(Filename, sep="\x01", names=clist ,iterator=True, chunksize=300000):