Python 3 Reading File and counting the lines in the fileAre dictionaries ordered in Python 3.6+?Read and print from a text file N lines at a time using a generator onlyUsing Selenium to find elements related to drop down list from another frame — PythonHow to split a file containing a speech into a list of words on python 3?Python 3 - function returns None type, yet print give correct outputtesting errors: comparing 2 filesCant read lines text file pythonLooping through JSON structured dataPython 3 Reading Relative Lines in a text document and convert to Pandas DFread AWS S3 GPG csv file to get row count

What is a "shilicashe?"

Why isn't pressure filtration popular compared to vacuum filtration?

Why didn't Thanos kill all the Dwarves on Nidavellir?

How can characters/players identify that a polymorphed dragon is a dragon?

Short story about Nobel Prize winning scientists that drop out when they realise they were incorrect

Some interesting calculation puzzle that I made

Why does this potentiometer in an op-amp feedback path cause noise when adjusted?

Was I subtly told to resign?

What specific instant in time in the MCU has been depicted the most times?

Can the Mage Hand cantrip be used to trip an enemy who is running away?

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

RPI3B+: What are the four components below the HDMI connector called?

Credit score and financing new car

How to trigger Authentification of Named Credential created via Apex

Are there any balance issues in allowing two half-feats to be taken without the Ability Score Increase instead of a feat?

Fast validation of time windows in a routing problem

Graduate student with abysmal English writing skills, how to help

Indesign - how to change the style of the page numbers?

How are mathematicians paid to do research?

This one's for Matthew:

How to deal with moral/legal subjects in writing?

What does the phrase "head down the rat's hole" mean here?

Historical experience as a guide to warship design?

Employers keep telling me my college isn't good enough - is there any way to fix this?



Python 3 Reading File and counting the lines in the file


Are dictionaries ordered in Python 3.6+?Read and print from a text file N lines at a time using a generator onlyUsing Selenium to find elements related to drop down list from another frame — PythonHow to split a file containing a speech into a list of words on python 3?Python 3 - function returns None type, yet print give correct outputtesting errors: comparing 2 filesCant read lines text file pythonLooping through JSON structured dataPython 3 Reading Relative Lines in a text document and convert to Pandas DFread AWS S3 GPG csv file to get row count






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Write a function named "count_lines" that takes a string as a parameter representing the name of a file to read and returns the number of lines in the file.



I honestly don't fully understand what needs to be done to complete the task.
I know that I need the parameter to open the file name stored in the parameter, but not sure how to do it or the for loop to count the lines in the file itself.



Thank you










share|improve this question




























    1















    Write a function named "count_lines" that takes a string as a parameter representing the name of a file to read and returns the number of lines in the file.



    I honestly don't fully understand what needs to be done to complete the task.
    I know that I need the parameter to open the file name stored in the parameter, but not sure how to do it or the for loop to count the lines in the file itself.



    Thank you










    share|improve this question
























      1












      1








      1








      Write a function named "count_lines" that takes a string as a parameter representing the name of a file to read and returns the number of lines in the file.



      I honestly don't fully understand what needs to be done to complete the task.
      I know that I need the parameter to open the file name stored in the parameter, but not sure how to do it or the for loop to count the lines in the file itself.



      Thank you










      share|improve this question














      Write a function named "count_lines" that takes a string as a parameter representing the name of a file to read and returns the number of lines in the file.



      I honestly don't fully understand what needs to be done to complete the task.
      I know that I need the parameter to open the file name stored in the parameter, but not sure how to do it or the for loop to count the lines in the file itself.



      Thank you







      python-3.6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 1:31









      Soap KillerzSoap Killerz

      275 bronze badges




      275 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          1














          def count_lines(filepath):
          with open(filepath, 'r+') as file:
          lines = file.readlines()
          return len(lines)





          share|improve this answer























          • Thanks, I didn't even realize that I could return the length of lines that were read like that.

            – Soap Killerz
            Mar 26 at 1:43











          • No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

            – ycx
            Mar 26 at 1:45










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348634%2fpython-3-reading-file-and-counting-the-lines-in-the-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          def count_lines(filepath):
          with open(filepath, 'r+') as file:
          lines = file.readlines()
          return len(lines)





          share|improve this answer























          • Thanks, I didn't even realize that I could return the length of lines that were read like that.

            – Soap Killerz
            Mar 26 at 1:43











          • No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

            – ycx
            Mar 26 at 1:45















          1














          def count_lines(filepath):
          with open(filepath, 'r+') as file:
          lines = file.readlines()
          return len(lines)





          share|improve this answer























          • Thanks, I didn't even realize that I could return the length of lines that were read like that.

            – Soap Killerz
            Mar 26 at 1:43











          • No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

            – ycx
            Mar 26 at 1:45













          1












          1








          1







          def count_lines(filepath):
          with open(filepath, 'r+') as file:
          lines = file.readlines()
          return len(lines)





          share|improve this answer













          def count_lines(filepath):
          with open(filepath, 'r+') as file:
          lines = file.readlines()
          return len(lines)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 1:40









          ycxycx

          1,8041 gold badge6 silver badges18 bronze badges




          1,8041 gold badge6 silver badges18 bronze badges












          • Thanks, I didn't even realize that I could return the length of lines that were read like that.

            – Soap Killerz
            Mar 26 at 1:43











          • No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

            – ycx
            Mar 26 at 1:45

















          • Thanks, I didn't even realize that I could return the length of lines that were read like that.

            – Soap Killerz
            Mar 26 at 1:43











          • No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

            – ycx
            Mar 26 at 1:45
















          Thanks, I didn't even realize that I could return the length of lines that were read like that.

          – Soap Killerz
          Mar 26 at 1:43





          Thanks, I didn't even realize that I could return the length of lines that were read like that.

          – Soap Killerz
          Mar 26 at 1:43













          No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

          – ycx
          Mar 26 at 1:45





          No problem. readlines() return a list. len() is simply reading the number of items in the list. If this is the answer that helped you, please mark as answer, thanks.

          – ycx
          Mar 26 at 1:45






          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348634%2fpython-3-reading-file-and-counting-the-lines-in-the-file%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          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