Python 使用文本文件检查文件是否存在

Python 使用文本文件检查文件是否存在,python,csv,operating-system,Python,Csv,Operating System,我有一个文件夹(分子),里面有许多代表不同分子的sdf文件(M00001.sdf、M00002.sdf等等)。我还有一个csv,其中每一行代表a分子(M00001、M00002等)。 我正在编写一个代码,以便在csv文件中的一行中获取分子文件夹中的文件 第一次尝试 import os path_to_files = '/path_to_folder/Molecules' # path to Molecules folder for files in os.listdir(path_to_fil

我有一个文件夹(分子),里面有许多代表不同分子的sdf文件(M00001.sdf、M00002.sdf等等)。我还有一个csv,其中每一行代表a分子(M00001、M00002等)。 我正在编写一个代码,以便在csv文件中的一行中获取分子文件夹中的文件

第一次尝试

import os
path_to_files = '/path_to_folder/Molecules' # path to Molecules folder

for files in os.listdir(path_to_files):
    names = os.path.splitext(files)[0] # get the basename (molecule name)
        with open('molecules.csv') as ligs: # Open the csv file of molecules names
            for hits in ligs:
                if names == hits:
                    print names, hits
                else:
                    print 'File is not here'

但是,这在命令行上不返回任何内容(实际上不返回任何内容)。此代码有什么问题?

我不确定这是否是最好的方法(我只知道以下代码适用于我的数据),但如果您的molecle.csv具有标准的csv格式,即“molecle1、molecle2、molecle3…”,您可以尝试以这种方式重新排列代码:

import os
import csv
path_to_files = '/path_to_folder/Molecules' # path to Molecules folder

for files in os.listdir(path_to_files):
    names = os.path.basename(files)
    names = names.replace(".sdf","")
    with open('molecules.csv','r') as ligs:
        content = csv.reader(ligs)
        for elem in content:
            for hits in elem:
                if names == hits:
                    print names, hits
                else:
                    print 'File is not here'

请参阅csv模块

我用一种相当野蛮的方法解决了这个问题

import os
import csv
import shutil

path_to_files = None # path to Molecules folder
new_path = None # new folder to save files
os.mkdir(new_path) # create the folder to store the molecules

hits = open('molecules.csv', 'r') 

ligands = []

for line in hits:
    lig = line.rstrip('\n')
    ligands.append(lig)

for files in os.listdir(path_to_files):
    molecule_name = os.path.splitext(files)[0]
    full_name = '/' + molecule_name + '.sdf'
    old_file = path_to_files + full_name
    new_file = new_path + full_name
    if molecule_name in ligands:
        shutil.copy(old_file, new_file)

它实际上是在打印else语句。@MarcosSantana请确保名称和命中数是相等的(因为如果名称为“molecule1.sdf”,命中数为“molecule1”,则会有else语句)。在正确的位置检查打印名称和打印命中率。只需检查即可。当我打印姓名时,它会显示molecle1,就像我打印hits一样。我真的不知道为什么它不起作用。当然,我只是通过搜索文件夹中的molecule1.sdf进行了检查,它就在那里。使用您的代码,内容中的每个元素都是一个列表(每个分子都是一个元素的列表,即分子名称),而不是一个字符串。也许这就是它不起作用的原因?在我的数据中,它起作用是因为点击是列表元素的一个元素,即字符串列表。因此,名称(即字符串)和每次点击(即字符串列表中的元素)之间存在比较(即==)。比较中是否有相同类型的变量?