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

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현