为什么 python os 模块没有给我正确的文件位置?
Why python os module not giving me the correct file location of the file?
I want to find get the location of files by just typing the file name and the correct extension
So in python we normally do this by importing the os module and then typing -:
os.path.abspath("file name.extension")
But my program is not giving me the correct file location I have written this code-:
from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.core.audio import SoundLoader
import os
helper_string = """
Screen:
BoxLayout:
orientation: "vertical"
ScrollView:
MDList:
id: scroll
"""
class MainApp(MDApp):
def build(self):
screen = Builder.load_string(helper_string)
return screen
def on_start(self):
for root, dirs, files in os.walk('C:/'):
for file in files:
if file.endswith('.mp3'):
required_file = file
the_location = os.path.abspath(required_file)
self.root.ids.scroll.add_widget(OneLineListItem(text=required_file, on_release=self.play_song))
# print(required_file)
def play_song(self, onelinelistitem):
# print('play:', onelinelistitem.text)
the_song_path = os.path.abspath(onelinelistitem.text)
sound = SoundLoader.load(the_song_path)
if sound:
sound.play()
print(the_song_path)
MainApp().run()
You can just ignore the SoundLoader.load code and all the other kivy code
Now when the code runs we have to select a file and it will print the file location on the console
But it is printing the wrong location and hence, the SoundLoader is not able to load the files...
For example a song in my computer is on this location -:
C:\Users\SAMARTH KUMAR PANDEY\Downloads\SHAREit\V1.12\music\Godzilla (feat. Juice WRLD) [Official Audio].mp3
But on the console it is printing this path -:
C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv\Godzilla (feat. Juice WRLD) [Official Audio].mp3
But the fact is that I do not have any songs in my C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv folder
Can anyone help me out please
os.path.abspath 将 return 来自当前目录的绝对路径,在您的情况下是“C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv”
你可能想要做的是像
一样加入你的遍历路径
the_location = os.path.join(root, required_file)
PS:您不需要创建对 required_file 的引用。您可以使用文件。
I want to find get the location of files by just typing the file name and the correct extension So in python we normally do this by importing the os module and then typing -:
os.path.abspath("file name.extension")
But my program is not giving me the correct file location I have written this code-:
from kivy.lang import Builder
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.core.audio import SoundLoader
import os
helper_string = """
Screen:
BoxLayout:
orientation: "vertical"
ScrollView:
MDList:
id: scroll
"""
class MainApp(MDApp):
def build(self):
screen = Builder.load_string(helper_string)
return screen
def on_start(self):
for root, dirs, files in os.walk('C:/'):
for file in files:
if file.endswith('.mp3'):
required_file = file
the_location = os.path.abspath(required_file)
self.root.ids.scroll.add_widget(OneLineListItem(text=required_file, on_release=self.play_song))
# print(required_file)
def play_song(self, onelinelistitem):
# print('play:', onelinelistitem.text)
the_song_path = os.path.abspath(onelinelistitem.text)
sound = SoundLoader.load(the_song_path)
if sound:
sound.play()
print(the_song_path)
MainApp().run()
You can just ignore the SoundLoader.load code and all the other kivy code
Now when the code runs we have to select a file and it will print the file location on the console But it is printing the wrong location and hence, the SoundLoader is not able to load the files...
For example a song in my computer is on this location -:
C:\Users\SAMARTH KUMAR PANDEY\Downloads\SHAREit\V1.12\music\Godzilla (feat. Juice WRLD) [Official Audio].mp3
But on the console it is printing this path -:
C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv\Godzilla (feat. Juice WRLD) [Official Audio].mp3
But the fact is that I do not have any songs in my C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv folder Can anyone help me out please
os.path.abspath 将 return 来自当前目录的绝对路径,在您的情况下是“C:\Users\SAMARTH KUMAR PANDEY\PycharmProjects\Kivy\venv”
你可能想要做的是像
一样加入你的遍历路径the_location = os.path.join(root, required_file)
PS:您不需要创建对 required_file 的引用。您可以使用文件。