0

我试图仅提取 zip 文件中子目录中的 .tsv 文件。我不断收到错误,因为文件路径看起来像这样

'C:\Users\hcho\OneDrive - Biocept\Schultz\Unzip\variant_oncomine_tsv_2020-02-25\SCRS_02JAN2020_B_v1_Filtered_2020-02-21_10:40:54-oncomine.tsv' 与双 '\'

而不是原始字符串

'C:\Users\hcho\OneDrive - Biocept\Schultz\Unzip\variant_oncomine_tsv_2020-02-25\SCRS_02JAN2020_B_v1_Filtered_2020-02-21_10:40:54-oncomine.tsv'

如何转换 os.getcwd() 的输出,使其作为文字字符串出现?

这是我的代码

import pandas as pd
import numpy as np
import os
import glob
import zipfile
import io
from datetime import date
import shutil

zip_files = []
for file in glob.glob("*.zip"):
    zip_files.append(file)

today = str(date.today())
new_dir_name = 'variant_oncomine_tsv'+'_'+today
if new_dir_name not in os.listdir():
    os.mkdir(new_dir_name)
cur_dir = os.getcwd()
destin = cur_dir+'\\'+new_dir_name
destin2 = r'variant_oncomine_tsv_2020-02-25'
def extract_oncomine_tsv(filename):
    with zipfile.ZipFile(filename) as z:
        for f in z.namelist():
            if '-oncomine.tsv' in f:
                filename = os.path.basename(f)
                #skip directories:
                if not filename:
                    continue

                #copy file (taken from zipfile's extract)
                source = z.open(f)
                target = open(os.path.join(destin,filename),'wb')
                with source, target:
                    shutil.copyfileobj(source, target)

for zips in zip_files:
    e = extract_oncomine_tsv(zips)

这是我收到的错误消息:

在此处输入图像描述

4

0 回答 0