我使用代碼從一個人Python:文件不存在。確保正確的文件名
import sys
try:
filename = sys.argv[1];
represent_points = int(sys.argv[2]);
delimeter = sys.argv[3];
if (delimeter=="space"):
delimeter = " ";
if (delimeter=="tab"):
delimeter = "\t";
f = open(filename+".txt","r");
except:
sys.exit("File not exists. Make sure of the right filename");
from string import *
import degree_sampling
degree_sampling.degree_sampling(filename,represent_points,delimeter);
但
[email protected]:~/KSCnet_Linux/Python_Code$ python trialrun.py datan 21
File not exists. Make sure of the right filename
這是不正確
[email protected]:~/KSCnet_Linux/Python_Code$ stat datan.txt
File: ‘datan.txt’
Size: 65 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 3670784 Links: 1
Access: (0664/-rw-rw-r--) Uid: (1000/ milenko) Gid: (1000/ milenko)
Access: 2016-04-08 10:54:23.695416155 -0300
Modify: 2016-04-08 10:54:23.695416155 -0300
Change: 2016-04-08 10:54:23.695416155 -0300
Birth: -
我該怎麼辦呢?
的'except'的捕捉每一個例外。所以不能確定問題出在打開的文件上。它可能甚至與'sys.argv [3]'不存在或任何東西。嘗試更改例外 –
您應該使用'except IOError:'專門捕獲文件不存在導致的錯誤 – wnnmaw