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?










0
















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









share|improve this question















marked as duplicate by Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















0
















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









share|improve this question















marked as duplicate by Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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














0












0








0









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









share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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


















  • 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













2 Answers
2






active

oldest

votes


















1














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()





share|improve this answer






























    0














    "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.






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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()





      share|improve this answer



























        1














        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()





        share|improve this answer

























          1












          1








          1







          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()





          share|improve this answer













          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()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 19:11









          blhsingblhsing

          41.3k41743




          41.3k41743























              0














              "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.






              share|improve this answer



























                0














                "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.






                share|improve this answer

























                  0












                  0








                  0







                  "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.






                  share|improve this answer













                  "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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 21 at 19:12









                  chepnerchepner

                  259k34249343




                  259k34249343













                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript