简体   繁体   English

'str' object 没有属性 'exists'/'is_file'

[英]'str' object has no attribute 'exists'/ 'is_file'

I am taking inputs from the user and at run time, I will check if there is a script exists in the current directory with the name passed by the user.我从用户那里获取输入,在运行时,我将检查当前目录中是否存在具有用户传递的名称的脚本。 If the file exists I will consider it as valid otherwise invalid.如果文件存在,我会认为它是有效的,否则无效。

parser = argparse.ArgumentParser(description='Main script')
parser.add_argument('--script', nargs='+',type=str)

script_list = process_args.script 

  for item in script_list:
      scn_script = os.path.join(os.getcwd(),item+'.py')
      if scn_script.exists():
         print(scn_script)
      else:
         print("Not exists")

In my current directory I am having abc.py, sample.py, test.py, names.py在我当前的目录中,我有 abc.py、sample.py、test.py、names.py

If the user passes entry,sample,hello, names through command line It should print sample.py exists names.py exists如果用户通过命令行传递 entry,sample,hello,names 它应该打印 sample.py exists names.py exists

ERROR 'str' object has no attribute 'exists'错误“str”object 没有属性“存在”

It's clear that type str has no attribute "exists"很明显 str 类型没有“存在”属性

dir(str) [' add ', ' class ', ' contains ', ' delattr ', ' doc ', ' eq ', ' format ', ' ge ', ' getattribute ', ' getitem ', ' getnewargs ', ' getslice ', ' gt ', ' hash ', ' init ', ' le ', ' len ', ' lt ', ' mod ', ' mul ', ' ne ', ' new ', ' reduce ', ' reduce_ex ', ' repr ', ' rmod ', ' rmul ', ' setattr ', ' sizeof ', ' str ', ' subclasshook ', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] dir(str) [' add ', ' class ', '包含', ' delattr ', ' doc ', ' eq ', '格式', ' ge ', ' getattribute ', ' getitem ', ' getnewargs ', ' getslice ',' gt ',' hash ',' init ',' le ',' len ',' lt ',' mod ',' mul ',' ne ',' new ',' reduce ',' reduce_ex ' ,' repr ',' rmod ',' rmul ',' setattr ',' sizeof ',' str ',' subclasshook ','_formatter_field_name_split','_formatter_parser','大写','center','count',' decode'、'encode'、'endswith'、'expandtabs'、'find'、'format'、'index'、'isalnum'、'isalpha'、'isdigit'、'islower'、'isspace'、'istitle' ,'isupper','join','ljust','lower','lstrip','partition','replace','rfind','rindex','rjust','rpartition','rsplit',' rstrip','split','splitlines','startswith','strip','swapcase','title','translate','upper','zfill']

Any Idea how can I get this issue fixed?任何想法我怎样才能解决这个问题?

Thanks谢谢

if os.path.exists(scn_script):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM