简体   繁体   English

Python 脚本在 Mac 上返回 FileNotFoundError,但不是 Linux

[英]Python script is returning FileNotFoundError on Mac, but not Linux

I am having an error with my script.我的脚本有错误。 I am on Linux (Ubuntu), and my script runs fine... but when I send it to someone who uses macOS, they get this error:我在 Linux (Ubuntu) 上,我的脚本运行良好......但是当我将它发送给使用 macOS 的人时,他们收到此错误:

FileNotFoundError: [Errno 2] No such file or directory: 'mytext.txt'` FileNotFoundError:[Errno 2] 没有这样的文件或目录:'mytext.txt'`

with open('mytext.txt', 'r') as f:
    file = f.readlines()

Of course, both me (and Google) thought it was because of relative path.当然,我(和谷歌)都认为这是因为相对路径。 I changed it to absolute path, but same error.我将其更改为绝对路径,但同样的错误。

import os
with open(os.getcwd() + '/mytext.txt', 'r') as f:
    file = f.readlines()

Well, that didn't work.好吧,那没有用。 Kept searching, and I tried this solution:继续搜索,我尝试了这个解决方案:

import os
path_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mytext.txt')

with open(path_name, 'r') as f:
    file = f.readlines()

...but that didn't work either. ...但这也不起作用。

Any ideas as to why this may be?关于为什么会这样的任何想法? Every iteration of the script worked for me, the Linux user, but not for the macOS user.脚本的每次迭代都对我(Linux 用户)有效,但不适用于 macOS 用户。 Is there something I'm missing about Linux and macOS Python script cross-compatibility, or something in my actual code I'm just doing wrong?关于 Linux 和 macOS Python 脚本交叉兼容性,我是否遗漏了什么,或者在我的实际代码中我做错了什么? And yes, I am sure there is a mytext.txt in the exact same directory as the actual script for both of us.是的,我确信在与我们俩的实际脚本完全相同的目录中存在一个mytext.txt Thanks for assisting me!谢谢你帮助我!

Edit : When trying to get help with this question in a different location, someone asked for the full error.编辑:当试图在不同的位置获得这个问题的帮助时,有人要求提供完整的错误。 In case it helps, here it is:如果有帮助,这里是:

Traceback (most recent call last):
  File "/Volumes/drive2/test/mytext.py", line 15, in <module>
    with open(path_name, 'r') as f:
IOError: [Errno 2] No such file or directory: '/Volumes/drive2/test/mytext.txt'

And to reiterate, it works fine for me (and other people who are not on macOS).重申一下,它适用于我(以及其他不在 macOS 上的人)。 Error only applies for the person trying to run this on macOS.错误仅适用于尝试在 macOS 上运行它的人。

So I tested this solution here that you proposed on a macOS with jupyter notebook:所以我在这里测试了你在带有 jupyter notebook 的 macOS 上提出的这个解决方案:

import os
with open(os.getcwd() + '/mytext.txt', 'r') as f:
    file = f.readlines() 

and it worked just fine for me, so this is not a macOS problem.它对我来说效果很好,所以这不是 macOS 问题。 The person that is trying to run this has to make sure that both the notebook/ python script as well as the.txt file are in the same folder though.试图运行它的人必须确保 notebook/python 脚本以及 .txt 文件都在同一个文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管存在 Mac,Python pandas read_csv 返回 FileNotFoundError - Python pandas read_csv returning FileNotFoundError despite existing Mac FileNotFoundError Python 脚本 - FileNotFoundError Python Script 在Linux或Windows计算机上为Mac编写python脚本 - Writing python script for a Mac on a linux or windows machine 使用单个 Python 脚本从 Linux、Mac 和 Windows 上的剪贴板复制数据 - Copy data from the clipboard on Linux, Mac and Windows with a single Python script 从命令行在MAC / Linux上调用python脚本-传递参数 - Calling python script on MAC/Linux from Command line - passing arguments FileNotFoundError: [Errno 2] 没有这样的文件或目录: &#39;geckodriver&#39;: &#39;geckodriver&#39; with GeckoDriver and Python in MAC OS - FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver' with GeckoDriver and Python in MAC OS 如何解决 FileNotFoundError: [Errno 2] Python 3.7/Mac 没有这样的文件或目录 - How to solve FileNotFoundError: [Errno 2] No such file or directory for Python 3.7/Mac Mac 操作系统; 蟒蛇3; FileNotFoundError: [Errno 2] 没有这样的文件或目录:&#39;&#39;。 如何解决? - Mac OS; Python3; FileNotFoundError: [Errno 2] No such file or directory: ''. How to fix it? FileNotFoundError:[错误2]没有这样的文件或目录:&#39;python&#39;:&#39;python&#39;在运行python脚本时 - FileNotFoundError: [Errno 2] No such file or directory: 'python': 'python' while running a python script python FileNotFoundError - python FileNotFoundError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM