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;
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
add a comment |
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
add a comment |
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
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
python-3.6
asked Mar 26 at 1:31
Soap KillerzSoap Killerz
275 bronze badges
275 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
def count_lines(filepath):
with open(filepath, 'r+') as file:
lines = file.readlines()
return len(lines)
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 alist
.len()
is simply reading the number of items in thelist
. If this is the answer that helped you, please mark as answer, thanks.
– ycx
Mar 26 at 1:45
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
def count_lines(filepath):
with open(filepath, 'r+') as file:
lines = file.readlines()
return len(lines)
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 alist
.len()
is simply reading the number of items in thelist
. If this is the answer that helped you, please mark as answer, thanks.
– ycx
Mar 26 at 1:45
add a comment |
def count_lines(filepath):
with open(filepath, 'r+') as file:
lines = file.readlines()
return len(lines)
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 alist
.len()
is simply reading the number of items in thelist
. If this is the answer that helped you, please mark as answer, thanks.
– ycx
Mar 26 at 1:45
add a comment |
def count_lines(filepath):
with open(filepath, 'r+') as file:
lines = file.readlines()
return len(lines)
def count_lines(filepath):
with open(filepath, 'r+') as file:
lines = file.readlines()
return len(lines)
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 alist
.len()
is simply reading the number of items in thelist
. If this is the answer that helped you, please mark as answer, thanks.
– ycx
Mar 26 at 1:45
add a comment |
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 alist
.len()
is simply reading the number of items in thelist
. 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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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