Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pycharm不会在我的macbook上读取txt文件吗?_Python - Fatal编程技术网

Python pycharm不会在我的macbook上读取txt文件吗?

Python pycharm不会在我的macbook上读取txt文件吗?,python,Python,我有一个代码,需要从文本文件中对比赛时间进行排序,这就是我到目前为止所拥有的 def get_sec(time_str): h, m = time_str.split(':') return int(h) * 3600 + int(m) * 60 with open("Race_Results_Sample.txt", "r")as myList: myList = myList.read() myList = [l.split(",") for l in myList.spl

我有一个代码,需要从文本文件中对比赛时间进行排序,这就是我到目前为止所拥有的

def get_sec(time_str):
h, m = time_str.split(':')
return int(h) * 3600 + int(m) * 60

with open("Race_Results_Sample.txt", "r")as myList:
    myList = myList.read()
    myList = [l.split(",") for l in myList.splitlines()]
    myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
    num, last, org, time = line
    place = []
    place.append(time)
    placenum = enumerate(sorted(place))
    print(place, placenum)
for rank, value in enumerate(sorted(place)):
    print(rank, value)
for line in myList:
    num, last, org, time = line
    new_time = get_sec(time)
    mile = round((((new_time/ 3.10686)/60)/60), 3)
    mile = str(mile)
    print ('{:<20s}{:<5s}{:<5s}{:<7s}{:<10s}'.format(last, num, org, 
    time, mile))
def获取秒(时间):
h、 m=时间分割(':')
返回整数(h)*3600+整数(m)*60
以open(“Race\u Results\u Sample.txt”、“r”)作为我的列表:
myList=myList.read()
myList=[l.splitlines()中l的l.split(“,”)]
myList=已排序(myList,key=lambda kv:kv[1])
对于myList中的行:
num,last,org,time=line
地点=[]
地点。追加(时间)
placenum=枚举(已排序(位置))
打印(位置、位置编号)
对于秩,枚举中的值(排序(位置)):
打印(等级、值)
对于myList中的行:
num,last,org,time=line
新建时间=获取秒(时间)
英里=圆(((新时代/3.10686)/60)/60),3)
英里=str(英里)

打印(“{:您的文件正在从
运行“/Users/jess/Library/Preferences/PyCharmEdu4.0/scratch/scratch.py”
,但您说该文件在您的桌面上。您需要向桌面文件传递一个

我建议将文件移动到
“/Users/jess/Library/Preferences/PyCharmEdu4.0/scratches/
。然后您可以在脚本中使用的相对引用中找到它,即


以open(“Race\u Results\u Sample.txt”、“r”)作为我的列表:


您还应该确保从该目录执行脚本。

您的桌面上有该文件。您的脚本是否在您的桌面文件夹中运行?Print
os.getcwd()
.Pycharm的scratch文件与您的项目不存储在同一目录中。请将文件移动到
/Users/jess/Library/Preferences/PyCharmEdu4.0/scratch/
或将代码移动到项目中的非scratch文件中。此外,您还可以使用始终有效的文件的绝对路径。我已将代码移动到非scratch文件中ch文件和我仍然收到相同的错误,这是脚本所在的位置,但工作目录不一定与脚本所在的目录相同。因此,我将代码更改为python文件而不是临时文件,并且仍然收到相同的错误。我建议更改代码以匹配您的帖子。接下来,将文件移动到
 Race_Results_Sample.txt
到文件夹
/Users/jess/Library/Preferences/PyCharmEdu4.0/scratch/
。之后,通过终端运行脚本。首先运行
cd/Users/jess/Library/Preferences/PyCharmEdu4.0/scratch/
。下一次运行
python-m scratch
。在您对python更加自信之前,我建议使用s简单文本编辑器(如Atom或Sublime)还有shell来做你的编程。这本书/网站《艰苦地学习Python》在第0章中谈到了这一点。Pycharm对于这个项目来说可能有点过分了。很抱歉,我在我的MacBook上找不到文件的移动位置。我已经找了又找,为了我的生命,我找不到它。好的,重新开始。在你的终端中:1。
cd~&mkdir-p src/race_res
2.
cd src/race_res
3.
touch race_results.py
4.
mv~/Desktop/race_results\u Sample.txt~/src/race_res
5.在一个简单的文本编辑器(Atom、Sublime、BBEdit)中打开
race_results.py
.6.将上面脚本的内容粘贴到该文件中。7.
python-m race\u results
///现在您正在以更基本的方式工作。将此项目的文件保存在
~/src/race\u res
中。在文本编辑器中进行更改。通过命令行执行。以后有足够的时间学习Pycharm。
Traceback (most recent call last):
  File 
"/Users/jess/Library/Preferences/PyCharmEdu4.0/scratches/scratch.py", 
line 5, in <module>
with open("Race_Results_Sample.txt", "r")as myList:
IOError: [Errno 2] No such file or directory: 'Race_Results_Sample.txt'