本文介绍了如何在Jupyter Notebook中指定文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jupyter notebook/google colab中指定文件路径时遇到问题.这是我发现的示例代码:

I have problem in specifying path of my file in jupyter notebook/google colab. This is the example code I found:

import csv

csvFile = 'myData.csv'
xmlFile = 'myData.xml'

csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')

我不知道上面代码的作者将myData.csv放在哪里,所以我尝试使用以下代码来定位我的文件:

I do not know where the author of the code above place the myData.csv, so I have tried this code to locate my file:

csvFile = 'C:\Users\...\myData.csv'

但我收到此错误:

SyntaxError:(unicode错误)"unicodeescape"编解码器无法解码位置2-3中的字节:\ UXXXXXXXX转义被截断了

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

我也尝试过以下代码:

csvFile = r'C:\Users\...\myData.csv'

但是我得到这个错误:FileNotFoundError:[错误2]没有这样的文件或目录:'C:\ Users \ ... \ myData.csv'

but I get this error:FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\...\myData.csv'

我的问题是:1.上面代码的作者将myData.csv放在哪里?2.如何指定文件位置?

My questions are:1. Where the author of the code above place the myData.csv?2. How can I specify the file location?

推荐答案

我通过将Google Colab安装到Google云端硬盘解决了这个问题.这是我安装到Google云端硬盘后的路径:

I solved this problem by mounting the Google Colab to Google Drive.This is the path after I mount to Google Drive:

csvFile = '/content/drive/My Drive/Colab Notebooks/myData.csv.txt'
xmlFile = '/content/drive/My Drive/Colab Notebooks/myData.xml'

这篇关于如何在Jupyter Notebook中指定文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:15