Posts: 9
Threads: 2
Joined: Aug 2022
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
Posts: 9
Threads: 2
Joined: Aug 2022
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.
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
Posts: 9
Threads: 2
Joined: Aug 2022
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.
Posts: 6,812
Threads: 20
Joined: Feb 2020
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
Posts: 9
Threads: 2
Joined: Aug 2022
(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 ...
1 2 |
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
Posts: 7,324
Threads: 123
Joined: Sep 2016
Aug-30-2022, 05:08 AM
(This post was last modified: Aug-30-2022, 05:08 AM by snippsat.)
(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
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 |
from pathlib import Path
dest = r 'C:\Test'
for path in Path(dest).rglob( '*.txt' ):
if path.is_file():
print (path)
|
Posts: 6,812
Threads: 20
Joined: Feb 2020
Aug-30-2022, 05:14 AM
(This post was last modified: Aug-30-2022, 05:14 AM by deanhystad.)
Why would you want to print? You said you want to iterate.
1 2 3 |
path = r "c:\whatever"
for filename in Path.glob(path, "*.md" ):
print (filename)
|
If you want to print a list of matching files you can do that too.
1 2 |
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
Posts: 9
Threads: 2
Joined: Aug 2022
Aug-30-2022, 05:36 AM
(This post was last modified: Aug-30-2022, 05:38 AM by PythonBorg.)
(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
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 |
from pathlib import Path
dest = r 'C:\Test'
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..
1 2 3 4 |
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...
Posts: 7,324
Threads: 123
Joined: Sep 2016
Aug-30-2022, 11:51 AM
(This post was last modified: Aug-30-2022, 11:51 AM by snippsat.)
(Aug-30-2022, 05:36 AM)PythonBorg Wrote:
1 2 3 4 |
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đź‘€
1 2 3 4 5 |
from pathlib import Path
dest = r "c:\test"
for path in Path(dest).glob( "*.md" ):
print (path)
|
PythonBorg likes this post
Posts: 9
Threads: 2
Joined: Aug 2022
(Aug-30-2022, 11:51 AM)snippsat Wrote: (Aug-30-2022, 05:36 AM)PythonBorg Wrote:
1 2 3 4 |
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đź‘€
1 2 3 4 5 |
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.
1 2 3 4 |
with open (path, "r" ) as f:
for i in path:
text = f.read()
html = markdown.markdown(text)
|
|