Python / CSV - 找不到文件错误

时间:2018-03-13 08:44:52

标签: python csv

一个基本问题,但我在目录C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv中有一个文件,比如file.csv,所以我写了,

df = pd.read_csv("C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv")

我收到错误 -

> FileNotFoundError: File
> b'C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv' does not exist

我试过

df = pd.read_csv("file.csv")

由于file.csv与我的python脚本位于完全相同的文件夹中,我仍然得到相同的错误。我不确定我做错了什么。我已经检查了python API,我已经尝试反转括号(所以使用\\而不是/),仍然无法正常工作。 谢谢。

3 个答案:

答案 0 :(得分:1)

尝试类似:

with open(my_file, 'r') as f_in:
    for line in f_in:
        print(line)

这样你可以检查Python是否可以打开文件。

答案 1 :(得分:0)

检查扩展程序public class BluetoothDeviceConnector : IDeviceConnector { private const string Tag = nameof(BluetoothDeviceConnector); private const int StateNone = 0; // we're doing nothing private const int StateConnecting = 2; // now initiating an outgoing connection private const int StateConnected = 3; // now connected to a remote device protected readonly BluetoothAdapter BluetoothAdapter; private readonly IMessageHandler _handler; private readonly string _mAddress; private ConnectThread _mConnectThread; private ConnectedThread _mConnectedThread; private int _mState; private readonly ILog _log; ///<summary> /// Prepare a new Bluetooth session. /// @param handler A Handler to send messages back to the UI Activity ///</summary> public BluetoothDeviceConnector(IMessageHandler handler, string address, ILog log) { _log = log; BluetoothAdapter = BluetoothAdapter.DefaultAdapter; _mState = StateNone; _handler = handler; _mAddress = address; } /// <summary> /// Set the current state of the connection /// @param state An integer defining the current connection state /// </summary> [MethodImpl(MethodImplOptions.Synchronized)] private void SetState(int state) { _log.Info(Tag + " setState() " + _mState + " -> " + state); _mState = state; } private BluetoothAdapter GetBluetoothAdapter() { return BluetoothAdapter.DefaultAdapter; } [MethodImpl(MethodImplOptions.Synchronized)] public void Connect() { var device = GetBluetoothAdapter().GetRemoteDevice(_mAddress); Connect(device); } ///<summary> /// Start the ConnectThread to initiate a connection to a remote device. /// @param device The BluetoothDevice to connect /// </summary> [MethodImpl(MethodImplOptions.Synchronized)] public void Connect(BluetoothDevice device) { _log.Info(Tag + " connecting to: " + device); // Cancel any thread attempting to make a connection if (_mState == StateConnecting) { if (_mConnectThread != null) { _mConnectThread.Cancel(); _mConnectThread = null; } } // Cancel any thread currently running a connection if (_mConnectedThread != null) { _mConnectedThread.Cancel(); _mConnectedThread = null; } // Start the thread to connect with the given device try { _mConnectThread = new ConnectThread(device, this); _mConnectThread.Start(); SetState(StateConnecting); _handler.SendConnectingTo(device.Name); } catch (SecurityException e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } catch (IllegalArgumentException e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } catch (NoSuchMethodException e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } catch (IllegalAccessException e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } catch (InvocationTargetException e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } catch (Exception e) { _log.Error(Tag + " Connect(BluetoothDevice device) :", e); } } ///<summary> /// Start the ConnectedThread to begin managing a Bluetooth connection /// @param socket The BluetoothSocket on which the connection was made /// @param device The BluetoothDevice that has been connected /// </summary> [MethodImpl(MethodImplOptions.Synchronized)] public void Connected(BluetoothSocket socket, BluetoothDevice device) { _log.Info(Tag + " connected"); // Cancel the thread that completed the connection if (_mConnectThread != null) { _mConnectThread.Cancel(); _mConnectThread = null; } // Cancel any thread currently running a connection if (_mConnectedThread != null) { _mConnectedThread.Cancel(); _mConnectedThread = null; } // Start the thread to manage the connection and perform transmissions _mConnectedThread = new ConnectedThread(socket, this); _mConnectedThread.Start(); SetState(StateConnected); _handler.SendConnectedTo(device.Name); } ///<summary> /// Stop all threads /// </summary> [MethodImpl(MethodImplOptions.Synchronized)] public void Disconnect() { _log.Info(Tag + " Disconnect"); if (_mConnectThread != null) { _mConnectThread.Cancel(); _mConnectThread = null; } if (_mConnectedThread != null) { _mConnectedThread.Shutdown(); _mConnectedThread.Cancel(); _mConnectedThread = null; } SetState(StateNone); _handler.SendNotConnected(); } [MethodImpl(MethodImplOptions.Synchronized)] public void SendAsciiMessage(string chars) { //Write((chars + "\n").GetBytes()); } ///<summary> /// Write to the ConnectedThread in an unsynchronized manner /// @param out The bytes to write /// @see ConnectedThread#Write(byte[]) /// </summary> private void Write(byte[] value) { // Create temporary object // Synchronize a copy of the ConnectedThread if (_mState != StateConnected) return; // Perform the write unsynchronized Task.Run(() => { _mConnectedThread.Write(value); }); } ///<summary> /// Indicate that the connection attempt failed and notify the UI Activity. /// </summary> private void ConnectionFailed() { SetState(StateNone); _handler.SendConnectionFailed(); _log.Info(Tag + " ConnectionFailed"); } ///<summary> /// Indicate that the connection was lost and notify the UI Activity. /// </summary> private void ConnectionLost() { SetState(StateNone); _handler.SendConnectionLost(); _log.Info(Tag + " ConnectionLost"); } /// <summary> /// This thread runs while attempting to make an outgoing connection /// with a device. It runs straight through; the connection either /// succeeds or fails. /// SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException /// </summary> private class ConnectThread : Thread { private readonly BluetoothSocket _mmSocket; private readonly BluetoothDevice _mmDevice; private readonly BluetoothDeviceConnector _deviceConnector; public ConnectThread(BluetoothDevice device, BluetoothDeviceConnector deviceConnector) { _mmDevice = device; BluetoothSocket tmp = null; _deviceConnector = deviceConnector; _deviceConnector._log.Info(Tag + " calling device.createRfcommSocket with channel 1 ..."); try { //tmp = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB")); //tmp = device.CreateRfcommSocketToServiceRecord(device.GetUuids()[0].Uuid); deviceConnector.BluetoothAdapter.CancelDiscovery(); var createRfcommSocket = JNIEnv.GetMethodID(device.Class.Handle, "createInsecureRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;"); var socket = JNIEnv.CallObjectMethod(device.Handle, createRfcommSocket, new JValue(1)); tmp = GetObject<BluetoothSocket>(socket, JniHandleOwnership.TransferLocalRef); _deviceConnector._log.Info(Tag + " calling device.createRfcommSocket with channel 2 ..."); var uuidList = device.GetUuids(); if (uuidList != null) { foreach (var uuid in uuidList) { try { _deviceConnector._log.Info(Tag + " connect with uuid: " + uuid); tmp = device.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid); tmp.Connect(); _deviceConnector._log.Info(Tag + " connect with uuid status: " + tmp.IsConnected); if (tmp.IsConnected) { _deviceConnector._log.Info(Tag + " uuid success " + uuid); break; } } catch (Exception ex) { // ignored } } } _deviceConnector._log.Info(Tag + " setting socket to result of createRfcommSocket"); _deviceConnector._log.Info(Tag + " setting socket Status" + tmp.IsConnected); } catch (Exception e) { _deviceConnector._log.Error(Tag + " ConnectThread", e); } _mmSocket = tmp; } public override void Run() { base.Run(); _deviceConnector._log.Info(Tag + " BEGIN mConnectThread"); //setName("ConnectThread"); // Always cancel discovery because it will slow down a connection _deviceConnector.BluetoothAdapter.CancelDiscovery(); // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a // successful connection or an exception _deviceConnector._log.Info(Tag + " Run() => " + _mmSocket.IsConnected); if (!_deviceConnector.BluetoothAdapter.IsDiscovering && !_mmSocket.IsConnected) { _deviceConnector._log.Info(Tag + " BEGIN mConnectThread 1"); _mmSocket.Connect(); _deviceConnector._log.Info(Tag + " BEGIN mConnectThread 2"); } } catch (IOException e) { _deviceConnector._log.Error(Tag + " ConnectThread => Run Function", e); _deviceConnector.ConnectionFailed(); try { _mmSocket.Close(); } catch (IOException e2) { _deviceConnector._log.Error(Tag + " unable to close() socket during connection failure", e2); } return; } // Reset the ConnectThread because we're done _deviceConnector._mConnectThread = null; // Start the connected thread _deviceConnector.Connected(_mmSocket, _mmDevice); _deviceConnector._log.Info(Tag + " END mConnectThread"); } [Obsolete("deprecated")] public override void Destroy() { try { _deviceConnector._log.Info(Tag + " Destory()"); _mmSocket?.Close(); _deviceConnector._log.Info(Tag + " Destroy"); base.Destroy(); } catch (IOException e) { _deviceConnector._log.Error(Tag + " close() of connect socket failed", e); } } public void Cancel() { try { _deviceConnector._log.Info(Tag + " Cancel()"); _mmSocket?.Close(); } catch (IOException e) { _deviceConnector._log.Error(Tag + " close() of connect socket failed", e); } } } ///<summary> /// This thread runs during a connection with a remote device. /// It handles all incoming and outgoing transmissions. /// </summary> private class ConnectedThread : Thread { private readonly BluetoothSocket _mmSocket; private readonly Stream _mmInStream; private readonly Stream _mmOutStream; private readonly BluetoothDeviceConnector _deviceConnector; public ConnectedThread(BluetoothSocket socket, BluetoothDeviceConnector deviceConnector) { _deviceConnector = deviceConnector; _mmSocket = socket; _deviceConnector._log.Info(Tag + " create ConnectedThread"); // Get the BluetoothSocket input and output streams try { _mmInStream = _mmSocket?.InputStream; _mmOutStream = _mmSocket?.OutputStream; } catch (IOException e) { _deviceConnector._log.Error(Tag + " temp sockets not created", e); } } private bool _stop; private readonly bool _hasReadAnything = false; public void Shutdown() { _stop = true; if (!_hasReadAnything) return; try { _deviceConnector._log.Info(Tag + " Shutdown ConnectedThread"); _mmInStream?.Close(); } catch (IOException e) { _deviceConnector._log.Error(Tag + " close() of InputStream failed.", e); } } public override void Run() { base.Run(); _deviceConnector._log.Info(Tag + " BEGIN mConnectedThread"); var reader = new Java.IO.BufferedReader(new Java.IO.InputStreamReader(_mmInStream)); while (!_stop) { try { var rfid = reader.ReadLine(); if (!string.IsNullOrEmpty(rfid?.Trim())) { _deviceConnector._handler.SendLineRead(rfid); } } catch (IOException e) { _deviceConnector._log.Error(Tag + " disconnected", e); _deviceConnector.ConnectionLost(); break; } } } ///<summary> /// Write to the connected OutStream. /// @param bytes The bytes to write /// </summary> public void Write(byte[] bytes) { try { _mmOutStream.Write(bytes, 0, bytes.Length); _deviceConnector._handler.SendBytesWritten(bytes); } catch (IOException e) { _deviceConnector._log.Error(Tag + " Exception during write", e); } } public void Cancel() { try { _deviceConnector._log.Error(Tag + " ConnectedThread() => Cancel()"); _mmSocket?.Close(); } catch (IOException e) { _deviceConnector._log.Error(Tag + " close() of connect socket failed", e); } } } } .csv.txt

答案 2 :(得分:0)

  

Window用户

使用正斜杠:

import pandas as pd
df_swing = pd.read_csv('D:/anaconda/envs/data/EDA/2008_all_states.csv')

上面显示的例子是绝对路径。它是从文件系统的基础到要加载的文件的完整路径,例如c:/Documents/Shane/data/test_file.csv。绝对路径将从驱动器说明符开始(在Windows中为c:/或d:/,在Mac或Linux中为“ /”)