Python Forum

Full Version: Installing Modules
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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
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
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
(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
(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)
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
(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...
(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)
(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)
Pages: 1 2