如果我不指定目录,Python Tkinter 应该使用默认目录

发布于 2025-01-17 22:10:14 字数 985 浏览 1 评论 0原文

我有以下程序: https://i.sstatic.net/tgzxz.png 。现在,如果我按“下载位置”按钮,则应将歌曲下载到所选目录中,这可以很好地工作。但是,如果我不按按钮,则应使用我选择的目录,“默认目录”,然后将文件移至此“默认目录” 移动文件的“选择下载位置按钮”代码的

def select_download_location():
global destination_source_new
destination_source_new = 
filedialog.askdirectory(initialdir=os.path.normpath(r'C:\Users\kevin\Music'))

代码下载位置:

filename_url = "y2meta.com"
format = ".mp3"
space = filename_url + " "
hyphen ="- "
quality_file_320 = " (320 kbps)"
backslash = "\\"
filename = '\\'+ space + hyphen +title+ quality_file_320 +format
# Location where the file is downloaded to
download_source = r'C:\Users\kevin\Downloads'
source = download_source + filename
# Location where the file should be moved to
destination = destination_source_new
# Move the file from the download folder to the destination folder
dest = shutil.move(source,destination)

I have the following program: https://i.sstatic.net/TgzXz.png. Now if I press the Download Location Button, then it should download the song to the chosen directory, this works perfectly. But if I don't press the button, then it should use a directory, which I chose, "a default directory" and move the file to this "default directory"
Code of the "Choose Download Location Button"

def select_download_location():
global destination_source_new
destination_source_new = 
filedialog.askdirectory(initialdir=os.path.normpath(r'C:\Users\kevin\Music'))

Code of the moving file to download location:

filename_url = "y2meta.com"
format = ".mp3"
space = filename_url + " "
hyphen ="- "
quality_file_320 = " (320 kbps)"
backslash = "\\"
filename = '\\'+ space + hyphen +title+ quality_file_320 +format
# Location where the file is downloaded to
download_source = r'C:\Users\kevin\Downloads'
source = download_source + filename
# Location where the file should be moved to
destination = destination_source_new
# Move the file from the download folder to the destination folder
dest = shutil.move(source,destination)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2025-01-24 22:10:14

您可以按如下方式指定目的地,如果目的地没有更改,它将采用默认目的地。

destination_source_new = ''
filename_url = "y2meta.com"
format = ".mp3"
space = filename_url + " "
hyphen = "- "
quality_file_320 = " (320 kbps)"
backslash = "\\"
filename = '\\' + space + hyphen + title + quality_file_320 + format
# Location where the file is downloaded to
download_source = r'C:\Users\kevin\Downloads'
source = download_source + filename
# Location where the file should be moved to
target  = r'C:\Users\kevin\Music'
destination = destination_source_new if destination_source_new  != '' else target
# Move the file from the download folder to the destination folder
print(source)
print(destination)
dest = shutil.move(source, destination)

输出:

source= C:\Users\kevin\Downloads\y2meta.com - title (320 kbps).mp3
destination= C:\Users\kevin\Music

you can assign destination as below and if destination doesn't changes it will take default destination.

destination_source_new = ''
filename_url = "y2meta.com"
format = ".mp3"
space = filename_url + " "
hyphen = "- "
quality_file_320 = " (320 kbps)"
backslash = "\\"
filename = '\\' + space + hyphen + title + quality_file_320 + format
# Location where the file is downloaded to
download_source = r'C:\Users\kevin\Downloads'
source = download_source + filename
# Location where the file should be moved to
target  = r'C:\Users\kevin\Music'
destination = destination_source_new if destination_source_new  != '' else target
# Move the file from the download folder to the destination folder
print(source)
print(destination)
dest = shutil.move(source, destination)

Output:

source= C:\Users\kevin\Downloads\y2meta.com - title (320 kbps).mp3
destination= C:\Users\kevin\Music
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文