i recently started to experimenting with python. after scripting my mathematics homework, i’m now able to write data to a file.
after pasting ’
'behind the string, it makes a newline, but the
is also visible. is there a way to make a newline and a text without
at the end of all lines?
text in my file at the moment:
('hello there', '
')
hi')
also everything i type , is gone the next time i run my script and add something new.
we can give you all the answers, but that won’t teach you anything
dreg = open("poost.txt", 'w')
for i in range(20):
dreg.write("yoooo hahah gaaar
")
dreg.close()
it seems that you are typing your code into what is called the ‘interpreter’ it starts lines with ‘>>>’ , this refreshes/flushes after you close the interpreter.
are you using blender as your coding tool? if-so you can create a new text file in the Text Editor, and save it as a somename.py , which you can later open to continue working on at a later date. But really, do some python tutorials
i know that
starts a new line. the problem so far is that the text in my python file is text, but in the .txt file it adds the brackets and ', where i want just simple text, like hello world, now there would stand (‘hello world’)
so:
file.write(‘hello world’) prints hello world in IDLE, but (‘hello world’) in notepad. is there a way to just write text, and not code tot a .txt file?
filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
file.write('
')
i = input('what should be in the file? ')
i = (i,'
')
i = str(i)
n = '
'
i = i+n
i = str(i)
file.write(i,)
read1 = file.read()
print(read1)
that’s the code
the line file.write’
'does nothing so far.
that’s the code : the line file.write(’
') does nothing so far.
firstly, the string you printing with print(read1) does contain the ’
’ so it should display that. You shouldn’t however see
that ‘silent’ formatting character if you use notepad to open that .txt
filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
file.write('
')
i = input('what should be in the file? ')
i = (i,'
')
i = str(i)
n = '
'
i = i+n
i = str(i)
file.write(i,)
read1 = file.read()
print(read1)
becomes
filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
# file.write('
') # this is not part of your problem
i = input('what should be in the file? ')
i = str(i) # was your input was an integer?
i = i +'
'
# the comma here should cause writing to continue on the same line,
# but if you are using '
' you end up with new lines anyway
file.write(i,)
read1 = file.read()
# this displays the exact string,
# including the invisible (from notepad) '
' formatting character.
print(read1)
thanks so far, going to try that code asap.
and i did read the documentation, r+ means you can both read and write to the file
update:
the new code from zeffii works for the
and brackets, they dissapeared. leaves me with the problem that
when i run the script, and type 1, then run the script again, and type 2, only the 2 is in the file, and the 1 has been deleted
getting this error
io.UnsupportedOperation: can’t do nonzero end-relative seeks
when i want it to be 1 byte before the end… with: file.seek(-1, 2) - 0 doen’t seem to work.
in the documentation theres -3, 2, saying going three bytes before the end of the file. i want to be at the end so -0 seems logical to me.
nope, there is no sign to 0 (unlike infinity which has a + or - sign). We start counting from 0 and after many hours of programming you will gradually appreciate this convenience.
experiment some more, see what .tell() does and see if seek(0,2) isn’t exactly what you are looking for. You can read the last character of the file to check if its a ’
’ (it’s considered one ‘character’)
psuedo code here :
open file
storevalue = seek(last byte)
if storevalue is not '
':
then if we want to write we first inject a '
' if we want a new line.
well i made a different approach. i thougt, i can also go to the beginning, create a new line, and type something there. odd thing is that when i prent like ‘1234567890’ , run the script again, and print hey, the output(data in the .txt file) is hey (enter) 67890
so the new data is still deleting old data, like insert is turned on…
with open(filepath,'r+') as file:
# force end
file.seek(0,2)
i = input('what should be in the file? ')
i = str(i) # was your input an integer?
i = i +'
'
# the comma here should cause writing to continue on the same line,
# but if you are using '
' you end up with new lines anyway
file.write(i,)