Why is this not reading what is in the file? Python [duplicate] The Next CEO of Stack OverflowRead from file after write, before closingHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?What does the “yield” keyword do?Does Python have a ternary conditional operator?What does if __name__ == “__main__”: do?How do I list all files of a directory?Does Python have a string 'contains' substring method?Why is reading lines from stdin much slower in C++ than Python?
Cannot shrink btrfs filesystem although there is still data and metadata space left : ERROR: unable to resize '/home': No space left on device
Spaces in which all closed sets are regular closed
Players Circumventing the limitations of Wish
Inductor and Capacitor in Parallel
Can I board the first leg of the flight without having final country's visa?
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
What CSS properties can the br tag have?
Why do we say 'Un seul M' and not 'Une seule M' even though M is a "consonne"
Is there a reasonable and studied concept of reduction between regular languages?
Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?
Is there a difference between "Fahrstuhl" and "Aufzug"?
Point distance program written without a framework
What is the process for purifying your home if you believe it may have been previously used for pagan worship?
From jafe to El-Guest
Is Nisuin Biblical or Rabbinic?
Why did early computer designers eschew integers?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Do scriptures give a method to recognize a truly self-realized person/jivanmukta?
Strange use of "whether ... than ..." in official text
Is it ok to trim down a tube patch?
In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?
Physiological effects of huge anime eyes
What would be the main consequences for a country leaving the WTO?
It is correct to match light sources with the same color temperature?
Why is this not reading what is in the file? Python [duplicate]
The Next CEO of Stack OverflowRead from file after write, before closingHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?What does the “yield” keyword do?Does Python have a ternary conditional operator?What does if __name__ == “__main__”: do?How do I list all files of a directory?Does Python have a string 'contains' substring method?Why is reading lines from stdin much slower in C++ than Python?
This question already has an answer here:
Read from file after write, before closing
3 answers
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
contents = f.read()
print(contents)
f.close
python
marked as duplicate by Aran-Fey
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Read from file after write, before closing
3 answers
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
contents = f.read()
print(contents)
f.close
python
marked as duplicate by Aran-Fey
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12
add a comment |
This question already has an answer here:
Read from file after write, before closing
3 answers
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
contents = f.read()
print(contents)
f.close
python
This question already has an answer here:
Read from file after write, before closing
3 answers
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
contents = f.read()
print(contents)
f.close
This question already has an answer here:
Read from file after write, before closing
3 answers
python
python
edited Mar 21 at 19:16
blhsing
41.3k41743
41.3k41743
asked Mar 21 at 19:09
Its LittlebossIts Littleboss
6
6
marked as duplicate by Aran-Fey
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Aran-Fey
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 21 at 19:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12
add a comment |
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12
add a comment |
2 Answers
2
active
oldest
votes
The file is opened in append mode so the file pointer is at the end of the file when you attempt to read it. You should do a file seek to the beginning of the file if you intend to read the entire file after writing:
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
f.seek(0)
contents = f.read()
print(contents)
f.close()
add a comment |
"a+"
opens the file in append/read mode, which means the file pointer is at the end of the file when first opened. The write appends to the file, but file pointer remains at the end after the write. You need to seek somewhere earlier in the file for f.read
to return anything.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The file is opened in append mode so the file pointer is at the end of the file when you attempt to read it. You should do a file seek to the beginning of the file if you intend to read the entire file after writing:
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
f.seek(0)
contents = f.read()
print(contents)
f.close()
add a comment |
The file is opened in append mode so the file pointer is at the end of the file when you attempt to read it. You should do a file seek to the beginning of the file if you intend to read the entire file after writing:
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
f.seek(0)
contents = f.read()
print(contents)
f.close()
add a comment |
The file is opened in append mode so the file pointer is at the end of the file when you attempt to read it. You should do a file seek to the beginning of the file if you intend to read the entire file after writing:
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
f.seek(0)
contents = f.read()
print(contents)
f.close()
The file is opened in append mode so the file pointer is at the end of the file when you attempt to read it. You should do a file seek to the beginning of the file if you intend to read the entire file after writing:
f = open("work.txt","a+")
a = input("number ")
print(a, "before file")
f.write(a)
f.seek(0)
contents = f.read()
print(contents)
f.close()
answered Mar 21 at 19:11
blhsingblhsing
41.3k41743
41.3k41743
add a comment |
add a comment |
"a+"
opens the file in append/read mode, which means the file pointer is at the end of the file when first opened. The write appends to the file, but file pointer remains at the end after the write. You need to seek somewhere earlier in the file for f.read
to return anything.
add a comment |
"a+"
opens the file in append/read mode, which means the file pointer is at the end of the file when first opened. The write appends to the file, but file pointer remains at the end after the write. You need to seek somewhere earlier in the file for f.read
to return anything.
add a comment |
"a+"
opens the file in append/read mode, which means the file pointer is at the end of the file when first opened. The write appends to the file, but file pointer remains at the end after the write. You need to seek somewhere earlier in the file for f.read
to return anything.
"a+"
opens the file in append/read mode, which means the file pointer is at the end of the file when first opened. The write appends to the file, but file pointer remains at the end after the write. You need to seek somewhere earlier in the file for f.read
to return anything.
answered Mar 21 at 19:12
chepnerchepner
259k34249343
259k34249343
add a comment |
add a comment |
Please add an explanation of the problem in your question even if it is the same as the title, not just code.
– Ctrl S
Mar 21 at 19:12