FileNotFoundError:[错误2]没有这样的文件或目录:'2MCREF〜E.JPG'

时间:2019-08-21 05:20:05

标签: python-3.x

我正在尝试打开与脚本不在同一位置的图像

代码:

import os
from PIL import Image

folder = '/Users/abc'
if not os.listdir(folder):
    print('Folder not found')
else:
    print('"{}" found'.format(folder))

for file in os.listdir(folder):
    print(file)
    data = Image.open(file,'r')
print('Done')

错误:

"/Users/abc" found
2MCREF~E.JPG
Traceback (most recent call last):
  File "img_to_s3bucket.py", line 25, in <module>
    data = Image.open(file,'r')
  File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/PIL/Image.py", line 2770, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '2MCREF~E.JPG'

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这可能是,因为您的工作目录和文件位置不同

您可以通过在以下命令中指定完整的文件路径来完成此操作:

  [WebMethod()]
        public DataTable insert_data_to_db_from_local(string partnumber, string srctcode, string dockcode,int pack,string error,string chk,string user,DateTime day,string ekb,string kbid)
        {
            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();
            strConnString = "Server=localhost;UID=sa;PASSWORD=12345678;database=bds_pp_srct;Max Pool Size=400;Connect Timeout=600;";
            strSQL.Append("INSERT INTO Hanheld (Part_Number,SRCT_Code,Dock_Code,Package,Error_Code,Chk_Type,LogUser,LogDate,ekb_order_no,Kanban_ID) VALUES ('" + partnumber + "','" + srctcode + "','" + dockcode + "','" + pack + "','" + error + "','" + chk + "','" + user + "','" + day + "','" + ekb + "','" + kbid + "') ");
            //strSQL.Append(" WHERE [SRCT_Code] = '" + strCusID + "' ");
            objConn.ConnectionString = strConnString;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;
            dtAdapter.Fill(ds);
            dt = ds.Tables[0];
            dtAdapter = null;
            objConn.Close();
            objConn = null;
            return dt;
        }

您可以通过以下方式实现:

data = Image.open(file,'r')