Python Forum
os.path.join() 'NoneType' confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.path.join() 'NoneType' confusion
#11
(Sep-22-2023, 05:26 PM)deanhystad Wrote: 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()

thank you for your help !
Reply
#12
To move a file or folder you would use shutils.move()

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

or you could use pathlib.Path.rename()

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

or you could use os.replace().

There are quite a few different ways to move a file depending on what you want to happen if there is already a file at the destination, or if you want to automatically create a folder if one doesn't already exist, or... join us not one of the ways that you can move a file.

To prevent madness, break your programs into smaller tasks. Write a program that reads a fixed named file. After you get that to work, write a program that reads the same file and creates an mp3 file. Did that work? Write a program that moves a file from one folder to another. Writing small programs to do specific things lets your focus on the one thing you are trying to learn. You will learn faster because you will make mistakes faster, and there is less code to sift through to find your mistakes.
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,224 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,486 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