PyRate生成DES模型输入文件遇OSError:文件未找到求助
问题:PyRate DES模型脚本报错找不到已存在的文件
按照PyRate的DES模型教程生成输入文件时,即使使用绝对路径且文件确实存在,仍触发以下错误:
OSError: 'C:\Users\User\Desktop\PyRate\example_recent.txt' not found.
执行命令及完整报错
(base) C:\Users\User\Desktop\\PyRate>python .\PyRateDES.py -fossil 'C:\\Users\\User\\Desktop\\PyRate\\example_fossil.txt' -recent 'C:\\Users\\User\\Desktop\\PyRate\\example_recent.txt' -wd 'C:\\Users\\User\\Desktop\\PyRate' -filename Example -bin_size 0.5 -rep 10 -trim_age 23.03 -taxon accepted_name -area cc -age1 max_ma -age2 min_ma Random seed: 1 Traceback (most recent call last): File "C:\Users\User\Desktop\PyRate\PyRateDES.py", line 189, in <module> desin_list, time = des_in(args.fossil, args.recent, args.wd, args.filename, taxon = args.taxon, area = args.area, age1 = args.age1, age2 = args.age2, binsize = args.bin_size, reps = reps, trim_age = args.trim_age, data_in_area = args.data_in_area) File "./pyrate_lib/lib_utilities.py", line 503, in des_in rece = np.genfromtxt(recent, dtype = str, delimiter='\t') File "C:\Users\User\anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1793, in genfromtxt fid = np.lib._datasource.open(fname, 'rt', encoding=encoding) File "C:\Users\User\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 193, in open return ds.open(path, mode, encoding=encoding, newline=newline) File "C:\Users\User\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 533, in open raise IOError("%s not found." % path) OSError: 'C:\\Users\\User\\Desktop\\PyRate\\example_recent.txt' not found.
文件存在性验证
已通过glob确认目标文件存在:
import glob print(glob.glob('C:\\Users\\User\\Desktop\\PyRate\\*.txt')) runfile('C:/Users/User/.spyder-py3/temp.py', wdir='C:/Users/User/.spyder-py3') ['C:\\Users\\User\\Desktop\\PyRate\\requirements.txt', 'C:\\Users\\User\\Desktop\\PyRate\\example_fossil.txt', 'C:\\Users\\User\\Desktop\\PyRate\\example_recent.txt']
解决方法
移除路径中的单引号:
Windows命令行不识别单引号作为路径包裹符,单引号会被当作路径的一部分传递给脚本,导致numpy无法定位文件。修改命令为:(base) C:\Users\User\Desktop\PyRate>python .\PyRateDES.py -fossil C:\Users\User\Desktop\PyRate\example_fossil.txt -recent C:\Users\User\Desktop\PyRate\example_recent.txt -wd C:\Users\User\Desktop\PyRate -filename Example -bin_size 0.5 -rep 10 -trim_age 23.03 -taxon accepted_name -area cc -age1 max_ma -age2 min_ma若路径包含空格,推荐用双引号包裹:
(base) C:\Users\User\Desktop\PyRate>python .\PyRateDES.py -fossil "C:\Users\User\Desktop\PyRate\example_fossil.txt" -recent "C:\Users\User\Desktop\PyRate\example_recent.txt" -wd "C:\Users\User\Desktop\PyRate" -filename Example -bin_size 0.5 -rep 10 -trim_age 23.03 -taxon accepted_name -area cc -age1 max_ma -age2 min_ma替换路径分隔符为正斜杠:
Windows系统中,Python脚本对正斜杠/的兼容性更好,可避免反斜杠的转义问题。修改命令如下:(base) C:\Users\User\Desktop\PyRate>python .\PyRateDES.py -fossil "C:/Users/User/Desktop/PyRate/example_fossil.txt" -recent "C:/Users/User/Desktop/PyRate/example_recent.txt" -wd "C:/Users/User/Desktop/PyRate" -filename Example -bin_size 0.5 -rep 10 -trim_age 23.03 -taxon accepted_name -area cc -age1 max_ma -age2 min_ma调试脚本的路径接收逻辑:
若以上方法无效,可临时修改pyrate_lib/lib_utilities.py第503行,添加打印语句确认实际传递的路径:print("Target file path:", recent) rece = np.genfromtxt(recent, dtype = str, delimiter='\t')运行脚本后查看输出的路径是否包含多余字符(如残留的单引号),再针对性调整命令行参数。
内容的提问来源于stack exchange,提问作者guibp
相关产品推荐
相关产品推荐

