python读取写入文件_将多行写入文件,然后使用Python读取

我刚刚用Python(一个存储代码片段的程序)完成了第一个正确的项目。

要做到这一点,我需要先写,然后读,多行到一个.txt文件。我在谷歌上搜索了很多东西,找到了一些关于写文件的东西(这些东西并没有真正起作用)。我目前正在工作的是一个函数,它读取多行输入的每一行,并在将其写入文件之前将其写入列表。我本以为我可以从文本文件中读取它,然后将每一行添加到一个列表中,然后使用while循环分别打印每一行,但不幸的是,这个循环不起作用。

在做了更多的研究之后,我决定问这里。这是我目前拥有的代码:'''

Project created to store useful code snippets, prehaps one day it will evolve

into something goregous, but, for now it's just a simple archiver/library

'''

#!/usr/local/bin/python

import sys, os, curses

os.system("clear")

Menu ="""

#----------- Main Menu ---------#

# 1. Create or edit a snippet #

# 2. Read a snippet #

# 0. Quit #

#-------------------------------#

\n

"""

CreateMenu ="""

#-------------- Creation and deletion --------------#

# 1. Create a snippet #

# 2. Edit a snippet #

# 3. Delete a snippet (Will ask for validation) #

# 0. Go back #

#---------------------------------------------------#

\n

"""

ReadMenu="""

#------ Read a snippet ------#

# 1. Enter Snippet name #

# 2. List alphabetically #

# 3. Extra #

# 0. Go Back #

#----------------------------#

"""

def readFileLoop(usrChoice, directory):

count = 0

if usrChoice == 'y' or 'n':

if usrChoice == 'y':

f = open(directory, 'r')

text = f.read()

f.close()

length = len(text)

print text

print length

raw_input('Enter to continue')

readMenu()

f.close()

elif choice == 'n':

readMenu()

def raw_lines(prompt=''):

result = []

getmore = True

while getmore:

line = raw_input(prompt)

if len(line) > 0:

result.append(line)

else:

getmore = False

result = str(result)

result.replace('[','').replace(']','')

return result

def mainMenu():

os.system("clear")

print Menu

choice = ''

choice = raw_input('--: ')

createLoop = True

if choice == '1':

return creationMenu()

elif choice == '2':

readMenu()

elif choice == '0':

os.system("clear")

sys.exit(0)

def create():

os.system("clear")

name = raw_input("Enter the file name: ")

dire = ('shelf/'+name+'.txt')

if os.path.exists(dire):

while os.path.exists(dire):

os.system("clear")

print("This snippet already exists")

name = raw_input("Enter a different name: ")

dire = ('shelf/'+name+'.txt')

print("File created\n")

f = open(dire, "w")

print("---------Paste code below---------\n")

text = raw_lines()

raw_input('\nEnter to write to file')

f.writelines(text)

f.close()

raw_input('\nSnippet successfully filled, enter to continue')

else:

print("File created")

f = open(dire, "w")

print("---------Paste code below---------\n")

text = raw_lines()

print text

raw_input('\nEnter to write to file')

f.writelines(text)

f.close()

raw_input('\nSnippet successfully filled, enter to continue')

def readMenu():

os.system("clear")

name = ''

dire = ''

print ReadMenu

choice = raw_input('--:')

if choice == '1':

os.system("clear")

name = raw_input ('Enter Snippet name: ')

dire = ('shelf/'+name+'.txt')

if os.path.exists(dire):

choice = ''

choice = raw_input('The Snippet exists! Open? (y/n)')

'''if not choice == 'y' or 'n':

while (choice != 'y') or (choice != 'n'):

choice = raw_input('Enter \'y\' or \'n\' to continue: ')

if choice == 'y' or 'n':

break'''

readFileLoop(choice, dire)

else:

raw_input('No snippet with that name exists. Enter to continue: ') #add options to retry, create snippet or go back

readMenu()

elif choice == '0':

os.system("clear")

print Menu

def creationMenu(): ###### Menu to create, edit and delete a snippet ######

os.system("clear")

print CreateMenu

choice = raw_input('--: ')

if choice == '1': ### Create a snippet

os.system("clear")

print create()

print creationMenu()

elif choice == '2':

os.system("clear") ### Edit a snippet

print ("teh editon staton")

raw_input()

print creationMenu()

elif choice == '3':

os.system("clear") ### Delete a snippet

print ("Deletion staton")

raw_input()

print creationMenu()

elif choice == '0': ### Go Back

os.system("clear")

######## Main loop #######

running = True

print ('Welcome to the code library, please don\'t disturb other readers!\n\n')

while running:

mainMenu()

######## Main loop #######

Tl;Dr:需要写入和读取多行文本文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值