如何使用Mac为Python输入和输出文件
所以我看了其他类似于我的问题的帖子,但我的代码仍然有错误。我应该在我的桌面上获取一个包含水果列表的文件,并将其导入python,让它自行排序,然后创建一个输出文件 我被告知outfile有语法错误,即使这是老师给我的代码。有人知道为什么我的代码不适合我吗如何使用Mac为Python输入和输出文件,python,python-2.7,Python,Python 2.7,所以我看了其他类似于我的问题的帖子,但我的代码仍然有错误。我应该在我的桌面上获取一个包含水果列表的文件,并将其导入python,让它自行排序,然后创建一个输出文件 我被告知outfile有语法错误,即使这是老师给我的代码。有人知道为什么我的代码不适合我吗 print "Program Started" #Input and output file given import os infile = open(os.path.expanduser("~/Desktop/unsorted_frui
print "Program Started"
#Input and output file given
import os
infile = open(os.path.expanduser("~/Desktop/unsorted_fruits.txt","r")
outfile = open("~/Desktop/sorted_fruits.txt","w")
#Reading of Input file
fruit=infile.read(50)
#Sorting of items in list
fruits.sort ()
for fruit in Fruits:
if fruit <> "\n": #If fruit is blank, skip the write
outfile.write(fruit) #otherwise write fruit to output file
print (fruit)
#Closing of the input and output file
infile.close()
outfile.close()
print "Program Completed"
打印“程序已启动”
#给定的输入和输出文件
导入操作系统
infle=open(os.path.expanduser(“~/Desktop/unsorted\u fruits.txt”,“r”)
outfile=open(“~/Desktop/sorted\u fruits.txt”,“w”)
#读取输入文件
水果=填充。读取(50)
#列表中项目的排序
水果。分类()
对于水果中的水果:
如果水果“\n”:#如果水果为空,请跳过写入
outfile.write(水果)#否则将水果写入输出文件
印刷品(水果)
#关闭输入和输出文件
infle.close()
outfile.close()
打印“程序已完成”
由于我不知道您收到了什么错误,也不知道您正在阅读的文件的结构,所以我写这篇文章的目的是向您展示您应该做什么
#import os -> imports should always go on top fyi
with open("~/Desktop/unsorted_fruits.txt","r") as infile:
sorted_file = sorted(infile.read().split())
with open("~/Desktop/unsorted_fruits.txt","r") as outfile:
for item in sorted_file:
outfile.write(item)
print "Program Completed"
希望这能对你有所帮助。你这里有一些错误,主要是语法错误。 具体而言:
缺少一个infle=open(os.path.expanduser(“~/Desktop/unsorted\u fruits.txt”,“r”)
围绕)
expanduser
- outfile不调用
,并且不会正确保存expanduser
只将其作为字符串读取,因此以后将其作为列表排序将不起作用fruit=infle.read(50)
-python区分大小写,因此水果必须是水果用于水果中的水果
这是一个有效的版本
print "Program Started"
#Input and output file given
import os
with open(os.path.expanduser("~/Desktop/unsorted_fruits.txt"),"r") as infile:
fruits = infile.read().splitlines()
outfile = open(os.path.expanduser("~/Desktop/sorted_fruits.txt"),"w")
#Reading of Input file
#fruits=infile.read(50)
#Sorting of items in list
fruits.sort()
for fruit in fruits:
if fruit <> "\n": #If fruit is blank, skip the write
outfile.write(fruit) #otherwise write fruit to output file
print (fruit)
#Closing of the input and output file
#infile.close()
outfile.close()
print "Program Completed"
打印“程序已启动”
#给定的输入和输出文件
导入操作系统
以open(os.path.expanduser(“~/Desktop/unsorted\u fruits.txt”),“r”)作为填充:
水果=infle.read().splitlines()
outfile=open(os.path.expanduser(“~/Desktop/sorted\u fruits.txt”),“w”)
#读取输入文件
#水果=填充。读取(50)
#列表中项目的排序
水果分类
对于水果中的水果:
如果水果“\n”:#如果水果为空,请跳过写入
outfile.write(水果)#否则将水果写入输出文件
印刷品(水果)
#关闭输入和输出文件
#infle.close()
outfile.close()
打印“程序已完成”
需要注意的是-这不是一个好的/干净的代码-它只是修复了您的错误您得到的错误是什么?您的代码是一团乱-您将输入文件读取为
水果
,然后尝试使用相同的变量名来迭代水果
,您使用不推荐使用的
操作符,您导入操作系统
,无需重新编译ason…我只是在使用分配给我的代码。这门课一团糟。它是在线的,这已经足够了。我根本无法从我的老师那里得到帮助。我得到的错误是outfile的语法错误。我做了你写的,这是我得到的错误。回溯(最近一次调用):File“/Users/shannongrice/Desktop/sort_fruits.py”,第7行,以打开(“~/Desktop/unsorted_fruits.txt”,“r”)作为输入文件:IOError:[Errno 2]没有这样的文件或目录:“~/Desktop/unsorted_fruits.txt”这是我现在的代码:print”程序启动了“没有这样的目录文件可能意味着您键入的路径错误。现在它说我有一个无效的语法,并突出显示了as before infle。您知道为什么吗?现在我得到了一个错误,因为有as before infle。您知道为什么吗?您能更具体地说明您的错误吗?如果您还没有修复关闭)
您仍然会遇到错误,这是您第一个问题的根源。复制并粘贴我在那里提供的所有内容,它应该可以正常工作。我使用IDLE,它显示无效语法,并突出显示与前面一样的内嵌,我不理解,因为您在提供的代码中使用了它,而另一个人使用了它,所以我假设它在Python中使用完全有效。它应该完全可以,将其另存为.py文件,并从命令行python myfile.py
运行它。你们都应该很好。而且——就工作而言,我发现自己是一名出色的编辑,在我看来,这比无所事事要好得多。