Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Installing Modules
#1
Hi There,

Have installed the Markdown module into Python3 using PIP.
it install without a problem... but when i try to use it, Python does not recognise it.
Have some markdown files that i would like to use.

import markdown
Reply
#2
It is now working.
did a bit of searching online and on my pc disk. Found out where the Markdown was installed and added a path to it.
Also reinstalled it via PyCharm and it is now working. Big Grin

Converted a markdown file to a html, found the code on this website, if anyone is interested

https://www.digitalocean.com/community/t...xt-to-html
Reply
#3
Have a whole directory of markdown files.
If I put the contents of the dir into a list , I could iterate thou the list...yes.

That is the next Project to work thou. Idea
Reply
#4
You do not need to put the contents of a directory into a list. Python already provides a way to iterate through a directory, or walk through a directory tree. Look at os.listdir() or os.walk().

https://docs.python.org/3/library/os.html

Better yet, use glob(). glob() lets you specify a search pattern for the filename, making it easy to do things like "return all jpeg files" or "return all files that start with "log".

https://docs.python.org/3/library/glob.html
Reply
#5
(Aug-29-2022, 03:02 PM)deanhystad Wrote: You do not need to put the contents of a directory into a list. Python already provides a way to iterate through a directory, or walk through a directory tree. Look at os.listdir() or os.walk().

https://docs.python.org/3/library/os.html

Better yet, use glob(). glob() lets you specify a search pattern for the filename, making it easy to do things like "return all jpeg files" or "return all files that start with "log".

https://docs.python.org/3/library/glob.html

Thanks...

if i use ...
path = r"c:\whatever"
contents = Path.glob(path,"*.md")]
how do i print of the list, all i get is a <generator object Path.glob at xxxxxx
Reply
#6
(Aug-30-2022, 04:30 AM)PythonBorg Wrote: how do i print of the list, all i get is a <generator object Path.glob at xxxxxx
from pathlib import Path

dest = r'C:\Test'
contents = Path(dest).glob("*.txt")
print(list(contents))
See that i use pathlib here recommend.
It's more common that not need a list,so then activate the generator bye looping over it.
from pathlib import Path

dest = r'C:\Test'
# <rglob> Recursively yield all existing files in all sub-directories
for path in Path(dest).rglob('*.txt'):
    if path.is_file():
        print(path)
Reply
#7
Why would you want to print? You said you want to iterate.
path = r"c:\whatever"
for filename in Path.glob(path,"*.md"):
     print(filename)   # Replace with something more interesting
If you want to print a list of matching files you can do that too.
path = r"c:\whatever"
filename_list = list(Path.glob(path,"*.md"))
Generators are like a list, but they "generate" one item at a time instead of having them all at the start. More and more Python built-in functions that used to return lists are now written as generators.
You can read about them here:

https://book.pythontips.com/en/latest/generators.html
Reply
#8
(Aug-30-2022, 05:08 AM)snippsat Wrote:
(Aug-30-2022, 04:30 AM)PythonBorg Wrote: how do i print of the list, all i get is a <generator object Path.glob at xxxxxx
from pathlib import Path

dest = r'C:\Test'
contents = Path(dest).glob("*.txt")
print(list(contents))
See that i use pathlib here recommend.
It's more common that not need a list,so then activate the generator bye looping over it.
from pathlib import Path

dest = r'C:\Test'
# <rglob> Recursively yield all existing files in all sub-directories
for path in Path(dest).rglob('*.txt'):
    if path.is_file():
        print(path)

Sorry should have said that i am using the Pathlib...
This is what i am using now..
path = r"c:\test"
contents = Path(path).glob("*.md")
for i in contents:
print(i,"\n")
I like to print, so that i can see what is happening...
Reply
#9
(Aug-30-2022, 05:36 AM)PythonBorg Wrote:
path = r"c:\test"
contents = Path(path).glob("*.md")
for i in contents:
print(i,"\n")
I like to print, so that i can see what is happening...
Yes sure,it's like my code but you don't need contents variable.
path is clearer than i in this case,and new line(\n) is automatic so not needed.
So like this and with working indentationđź‘€
from  pathlib import Path

dest = r"c:\test"
for path in Path(dest).glob("*.md"):
    print(path)
PythonBorg likes this post
Reply
#10
(Aug-30-2022, 11:51 AM)snippsat Wrote:
(Aug-30-2022, 05:36 AM)PythonBorg Wrote:
path = r"c:\test"
contents = Path(path).glob("*.md")
for i in contents:
print(i,"\n")
I like to print, so that i can see what is happening...
Yes sure,it's like my code but you don't need contents variable.
path is clearer than i in this case,and new line(\n) is automatic so not needed.
So like this and with working indentationđź‘€
from  pathlib import Path

dest = r"c:\test"
for path in Path(dest).glob("*.md"):
    print(path)

Thanks for that.....
I have about 15 files in the directory, so if I do the following that will convert from md to html.

	with open(path,"r") as f:
		for i in path:
			text = f.read()
			html = markdown.markdown(text)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Trouble installing modules/libraries and getting Notepad++ to show cyrillic letters Dragiev 6 2,375 Jul-24-2022, 12:55 PM
Last Post: Dragiev
  Installing Modules / packages Oshadha 1 1,778 Feb-05-2021, 08:04 PM
Last Post: Jeff900
  installing third-party modules shabux 5 3,575 Apr-13-2020, 12:41 AM
Last Post: Larz60+
  3rd Party Modules - Installing martyl 9 5,884 Jun-15-2018, 01:49 AM
Last Post: martyl
  pip and installing modules deep_logic 2 6,151 Jun-25-2017, 03:23 AM
Last Post: deep_logic
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,574 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020