使用Visual Studio代码时,可以打开文件-Python
每当我尝试在Python中运行VisualStudio中的程序时,涉及打开文件并读取文件的简单过程,或使用PD.Pandas创建数据框,我会遇到以下错误:
filenotfounderror:[errno 2]没有这样文件或目录:'file -name.csv'
我已经检查了几个不同的路径,例如尝试通过不同的路径将V与Conda虚拟环境(我通过Anaconda软件包使用Python)连接 - 要么启动应用程序从Anaconda Navigator内部或通过内部终端中的命令行,在所有情况下都会遇到相同的错误。
我尝试运行的代码的一个非常简单的示例是以下内容,它使用PANDAS打开了一个使用.json扩展名的文件,并将其转换为.csv文件:
import pandas as pd
dataframe = pd.read_json('original-file-name.json', lines = True)
dataframe.to_csv('converted-file.csv', index=False, encoding='utf-8')
我相信我对VS代码的功能是我的一些愚蠢,因为在另一个IDE(例如Spyder)中运行上面提到的相同代码时,相同的作品。
感谢任何形式的帮助或指导!提前致谢
Whenever I try to run a program in VisualStudio in python that involves the simple procedure of opening a file and reading it, or creating a DataFrame with pd.pandas I come across the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'file-name.csv'
I have already checked several different paths, like trying to connect VS with the conda virtual environment (I use python through the Anaconda package) through different paths - either launching the application from within anaconda navigator or through command lines in the internal terminal, and in all cases encounter the same error.
A very simple example of code that I tried to run was the following, which opens a file with a .json extension and converts it to a .csv file using pandas:
import pandas as pd
dataframe = pd.read_json('original-file-name.json', lines = True)
dataframe.to_csv('converted-file.csv', index=False, encoding='utf-8')
I believe that it is some foolishness of mine regarding the functioning of VS Code, since when running the same code mentioned above in another IDE, such as Spyder, the same works.
I appreciate any kind of help or guidance! thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答只是为了解决这个问题,如果任何人陷入我犯的新秀错误,@boargules对上述主题的评论也可能会为您解决:p
在试图在当前IDE中打开文件时,请检查路径问题,您应该检查根据推荐的
OS
模块,获取当前工作目录(CWD)的工作代码示例是输出:
当前工作目录:/your- Current-working目录
如果您想更改当前的工作目录,则可以使用
os.chdir()
(仅将单个参数作为新目录路径),然后您可以自动化您的代码使其在保存.py代码的目录中运行(显然,这是保存.json或.csv文件的目录,因为这是您实际上想打开的)示例:
输出
当前的工作目录
C:\ users \ user \ desktop \ file
当前工作目录之后
C:\ users \ user \ desktop
应该执行(至少它适用于我的原始问题!)
Answering just to close the question, in case anyone falls into the same rookie mistake I did, @BoarGules comments on the topic above will probably solve it for you too :P
For path problems while trying to open files in your current IDE you should check as recommended the
os
module, a working code example to get the current working directory (CWD) isOUTPUT:
Current working directory: /your-current-working-directory
In case you want to change your current working directory you can use the
os.chdir()
(it only takes a single argument as a new directory path) and then you can automate your code so that it runs in the directory where your .py code is saved (obviously this is the same directory where your .json or .csv file is saved, since that's the one you're actually wanting to open)Example:
OUTPUT
Current working directory before
C:\Users\User\Desktop\file
Current working directory after
C:\Users\User\Desktop
That should do (at least it works for my original problem!)