如何从字符串

问题描述:

逃避单引号这是我的问题:如何从字符串

fpaths=os.listdir(ligand_names_list[0].replace("'", "\\'")) 
OSError: [Errno 2] No such file or directory: "5-iodoindirubin-3\\'-oxime" 

有一个名为5-iodoindirubin-3'-oxime文件,但我不能让os.listdir()找到它。这里的另一个尝试我做了这个线程Adding backslashes without escaping [Python]启发:

fpaths=os.listdir(ligand_names_list[0].__ repr __()) 
OSError: [Errno 2] No such file or directory: '"5-iodoindirubin-3\'-oxime"' 

在这种情况下,问题是全球领先的单引号,我不知道如何删除它们。任何想法?

你不必逃脱任何东西;将字符串文字输入到代码中时,您只需要转义一旦该字符串有正确的值,您可以照原样使用它。

该文件是在当前目录还是在其他目录中? (当前目录与您的Python脚本所在的目录是相同的,除非您已更改它。)如果它不在当前目录中,那就可以解释为什么找不到它。

(你说这是一个文件,我希望它是一个目录,因为你要求它os.listdir() ...)

+0

事实上,这是一个目录,你说得对,没有必要躲避单引号在这里。 – tevang