Python Forum
os.path.join() 'NoneType' confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.path.join() 'NoneType' confusion
#1
Hi there !

this is an exercise to just save an audio file from a pdf. I want this file to be moved to a specific folder.

import pyttsx3, PyPDF2, os,os.path, glob
from PyPDF2 import PdfReader
from typing import List

speaker = pyttsx3.init()
#i deleted part of the path in the example for privacy matter
program_path = "C:\\ . . \\Python learning center\\automating_1"
save_path = "C:\\ . . \\Python learning center\\automating_1\\Audio"

for files in glob.glob("*.pdf"):
    print(files)
#here we enter the name of the pdf file that we want to be used for audio file
bookname = input("choose a file. . ")

reader = PdfReader(bookname, "rb")
number_of_pages = len(reader.pages)
page = reader.pages[0]
text = page.extract_text()
clean_text = text.strip().replace('\n', '')
print(clean_text)

audio_name = input("name the audio file: ")
audio_file = speaker.save_to_file(clean_text, audio_name + '.mp3')
move_file = os.path.join(program_path, save_path, audio_file)

speaker.runAndWait()

speaker.stop()
so the paths AND the audio file name are str type. but here's what i get :

Error:
join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
where is my mistake ?
Reply
#2
What does speaker.save_to_file() return ?
Reply
#3
(Sep-22-2023, 10:30 AM)Gribouillis Wrote: What does speaker.save_to_file() return ?

it creates the audio file, and it is automatically placed in the program folder.

but i'd like to have this file moved in a children folder (i means, which is in my program folder)
Reply
#4
(Sep-22-2023, 11:11 AM)gowb0w Wrote: it creates the audio file, and it is automatically placed in the program folder.
I didn't ask "what does it do?", but "what does it return?", which is very different.
Reply
#5
How can i find that ? i'm a beginner, i don't know all the fundamentals ^^
Reply
#6
(Sep-22-2023, 03:39 PM)gowb0w Wrote: How can i find that ?
Try
print(audio_file)
or
print(type(audio_file))
for example.

A function can do something and return None. This is very common. For example the print() function
Reply
#7
[/quote]
Try
print(audio_file)
or
print(type(audio_file))
for example.

A function can do something and return None. This is very common. For example the print() function
[/quote]

and you wasn't wrong lol

<class 'NoneType'>
Reply
#8
so how is that possible ?

the file name is supposed to be a string type, isnt it ?
Reply
#9
Looking at the source (I don't want to install the package), Engine.save_to_file() returns None. That makes this code nonsinsical.
audio_file = speaker.save_to_file(clean_text, audio_name + '.mp3')
What made you think it returned a str?

What kind of magic was this meant to do?
move_file = os.path.join(program_path, save_path, audio_file)
Was this supposed to move a file audiofile from program_path to save_path? Did you read the documentation for os.path.join? os.path.join is normally used to append a filename to a file path.

What is the purpose of this?
program_path = "C:\\ . . \\Python learning center\\automating_1"
save_path = "C:\\ . . \\Python learning center\\automating_1\\Audio"
You should rarely hard code file paths, especially not a path that appears to be in a user folder. You cannot move your file. Nobody else can run your program. Use relative paths. Normally I use no path, or "." if a path is required. If you want to use the folder that contains your program file, use "__file__" in your program. Python replaces "__file__" with the full filename
current_directory = pathlib.Path(".")
program_folder = "pathlib.Path(__file__).parent
Your program saved the mp3 file in the current directory. This might be the program_path, but it might not. You don't need to know the program path anyway. Save the file directly to the save folder.

You should hardly ever import "os". Most of the things "os" was used for now have os independent packages that work better. Instead of os.path, us pathlib. And don't use backslashes in paths. There is just too much messiness with \ being a separator, the start of an escape sequence, having special meaning in regular expressions... Use forward slash instead. Windows doesn't care what slash you use.
import pyttsx3
from PyPDF2 import PdfReader
from pathlib import Path

# Select a pdf file.  Extract first page.
print(*Path().glob("*.pdf"), sep="\n")
bookname = input("choose a file. . ")
reader = PdfReader(bookname, "rb")
number_of_pages = len(reader.pages)
page = reader.pages[0]
text = page.extract_text()
clean_text = text.strip().replace("\n", "")

# Convert pdf text to speech.  Save as mp3.  Read text.
speaker = pyttsx3.init()
audio_name = input("name the audio file: ")
save_path = Path(__file__).parent / "Audio"
audio_file = speaker.save_to_file(clean_text, save_path / f"{audio_name}.mp3")
speaker.runAndWait()
Reply
#10
all i want to do is to move the created audio file to a second folder in the main program folder

just an exercise to train myself

i'll become mad Confused
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,222 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  os.path.join - errors out tester_V 4 2,720 Nov-29-2020, 08:57 AM
Last Post: DeaD_EyE
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,485 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  os.path.join qmfoam 2 2,387 Nov-08-2020, 04:03 PM
Last Post: qmfoam
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,250 Feb-14-2019, 08:34 PM
Last Post: woooee
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,780 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

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