正如标题所说,我发现很难每次使用python编写新行文本。
当我写一个文件时,它只是用刚才写的东西替换第一行。
我尝试过使用f.write(WhatPhone +' \ n')但它似乎无法正常工作,我做错了什么?我尝试过很多东西。
WhatPhone = input('What Model would you like to be supported? Please Use the Format [brand] / [model] - ')
AddSupportedModels.append(WhatPhone)
with open('AddSuggestedPhone.txt','w') as out:
WhatPhone = WhatPhone
out.write('{}\n'.format(WhatPhone))
注意:我还尝试过其他一些可以在上面的代码中找到的内容:
这是我的整个计划:
import time
import os
import sys
ContactManufacturer = "As there are no other solutions, we suggest you now contact your phones manufacturer."
AddSupportedModels = []
print('#################################################')
print('Welcome to our Automated Troubleshooting Service!')
print('#################################################')
print('')
print("Hello and welcome to our Automated Troubleshooting program!\n")
print("When Answering questions please...")
time.sleep(0.5)
print("1. Answer using YES and NO answers. Answers are not case sensitive")
print("2. Answer all questions fully and truthfully. The program may give false answers if you do not follow instructions.\n\n")
def AreYouReady():
global AreYouReady
AreYouReady = input('Are you ready for the support? - ')
def CorrectModel():
global CorrectModel
CorrectModel = input('You have selected '+WhatModel+' as your model, is this correct? - ').lower()
def WhatBrand():
global WhatBrand
WhatBrand = input('What Brand of Phone do you have? - ').lower()
def WhatModel():
global WhatModel
WhatModel = input('What Model do you have? - ').lower()
def DidThisWork():
global DidThisWork
DidThisWork = input('\n\nDid this solution work? - ').lower()
def SuggestModel():
global SuggestModel
SuggestModel = input('Would you like to suggest a model of your device to be added to the supported model list? - ').lower()
start = input("Please enter '1' to continue to the support. Please enter '2' to view suggestions for supported phones.")
if start == '2':
simplejson.load(f)
#What Phone?
if start == '1':
WhatPhone = input('Do you have Android or an iPhone? - ').lower()
#Android
if WhatPhone == 'android':
WhatBrand()
if WhatBrand == 'samsung':
WhatModel()
if 's5' in WhatModel or 's5 edge' in WhatModel or 's6' in WhatModel or 's6 edge' in WhatModel or 's7' in WhatModel or 's7 edge' in WhatModel:
CorrectModel()
elif "note 7" in WhatModel:
print("Return this device to Samsung right away as it is extremely faulty and may cause a lot of damange!")
else:
print("Selected Model not supported!")
SuggestModel()
if 'yes' in SuggestModel:
WhatPhone = input('What Model would you like to be supported? Please Use the Format [brand] / [model] - ')
AddSupportedModels.append(WhatPhone)
with open('AddSuggestedPhone.txt','w') as out:
WhatPhone = WhatPhone
out.write('{}\n'.format(WhatPhone))
if CorrectModel == 'yes':
AreYouReady()
if WhatBrand == 'lg':
WhatModel()
if 'g6' in WhatModel or 'g5' in WhatModel or 'k10' in WhatModel or 'k4' in WhatModel:
CorrectModel()
else:
print("Selected Model not Supported!")
if CorrectModel == 'yes':
AreYouReady()
if WhatBrand == 'google':
WhatModel()
if 'pixel' in WhatModel or 'pixelxl' in WhatModel:
CorrectModel()
else:
print("Selected Model not supported!")
if CorrectModel == 'yes':
AreYouReady()
if WhatBrand == 'hauwei':
WhatModel()
if 'p10' in WhatModel or 'p10 plus' in WhatModel or 'mate 9' in WhatModel or 'p8 lite' in WhatModel or 'nova' in WhatModel or 'p9 plus' in WhatModel or 'p9 lite' in WhatModel:
CorrectModel()
else:
print('Selected Model not supported!')
if CorrectModel == 'yes':
AreYouReady()
if WhatBrand == 'nexus':
WhatModel()
if '5' in WhatModel or '6' in WhatModel or '5x' in WhatModel or '6p' in WhatModel:
CorrectModel()
else:
print('Selected Model not supported!')
if CorrectModel == 'yes':
AreYouReady()
if AreYouReady == 'yes':
yourproblem = input('What is your problem? - ')
if 'screen' in yourproblem and 'not' in yourproblem:
f = open("Solutions.txt","r")
sol = f.readlines()
print(sol[0])
print("\n",sol[1])
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print(sol[2])
elif DidThisWork == 'yes':
exit()
if 'battery' in yourproblem and 'hot' or 'overheating' in yourproblem:
print('Please power off your device for 10 minutes to let it cool down.')
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print('Please use MalwareBytes to run a virus scan on your device. Viruses can overheat your battery.')
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print('Please factory reset your phone as this may be some sort of software causing the problem. Remember to backup important data so you dont lose it.')
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print(ContactManufacturer)
if 'phone' in yourproblem and 'slow' in yourproblem:
print('Firstly, please reboot your device and see if this solves the problem.')
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print("Please try uninstalling unnecessary applications from your phone.")
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print('Please use MalwareBytes to run a virus scan on your phone as viruses can slow down your phone.')
time.sleep(5)
DidThisWork()
if DidThisWork == 'no':
print(ContactManufacturer)
如果有人看过它并提出要改变的事情以及我做错了的事情,我也会赞美它,因为我对python c很新:
先谢谢!
答案 0 :(得分:0)
open()返回一个文件对象,最常用的有两个 参数:
open(filename, mode)
。 [...]第一个参数是包含文件名的字符串。第二个参数是另一个字符串,其中包含一些描述文件使用方式的字符。只读文件时,模式可以是'r'
,仅写入'w'
(将删除同名的现有文件),'a'
打开要追加的文件;写入文件的任何数据都会自动添加到最后。'r+'
打开文件进行读写。 mode参数是可选的;如果省略'r'
将被假设。