在Argparse中传递原始字符串以定义Python中的路径

发布于 2025-01-27 02:34:58 字数 1588 浏览 3 评论 0原文

from importlib.resources import path
import os, shutil, pathlib, fnmatch
import argparse
import time

parser = argparse.ArgumentParser()
parser.add_argument('--src', type=str)
parser.add_argument('--dst', type=str)
parser.add_argument('--pattern', type=str)
args = parser.parse_args()

def move_dir(src: str, dst: str, pattern: str = '*'):
    if not os.path.isdir(dst):
        pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
    for f in fnmatch.filter(os.listdir(src), pattern):
        shutil.move(os.path.join(src, f), os.path.join(dst, f))


move_dir(args.dst(), args.src(),  args.pattern())
time.sleep(5)
move_dir(r"C:\Users\user\Downloads", r"C:\Users\user\Documents\pdf", r"*.pdf")

创建一个脚本以根据特定目录中的模式移动文件。最初,当我通过将输出RAW进行硬编码和解决时,遇到了原始的字符串错误:如何在Python中移动文件?

问题 当我使用硬码测试时,必须将R放在字符串之前,我想测试从旧目录中移动,然后重新复制。 (在没有第二行到最后2行的情况下,正确运行了一次代码一次)

认为当返回的错误时,我现在无法做到这一点。

C:\Users\user\Documents\GitHub\personalUtilities>python downloadOrganizer.py --src "C:\Users\user\Documents\pdf" --dst "C:\Users\user\Downloads" --pattern "*.pdf"  
usage: downloadOrganizer.py [-h] [--src SRC] [--dst DST] [--pattern PATTERN]
downloadOrganizer.py: error: argument --src: invalid path value: 'C:\\Users\\user\\Documents\\pdf'

我 如果目录存在,但这无法解决我的问题,如果我传递了错误的字符串类型,并且对此主题中的任何文章似乎都在回答另一个问题或解决其他问题。

那么,从Argparse传递源和目标目录字符串的正确方法是什么? 尝试将类型更改为SRC和DST的路径,但没有更改任何内容。在引用字符串之前,试图将R添加到CLI中的Arg,这使目录越来越错误!

from importlib.resources import path
import os, shutil, pathlib, fnmatch
import argparse
import time

parser = argparse.ArgumentParser()
parser.add_argument('--src', type=str)
parser.add_argument('--dst', type=str)
parser.add_argument('--pattern', type=str)
args = parser.parse_args()

def move_dir(src: str, dst: str, pattern: str = '*'):
    if not os.path.isdir(dst):
        pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
    for f in fnmatch.filter(os.listdir(src), pattern):
        shutil.move(os.path.join(src, f), os.path.join(dst, f))


move_dir(args.dst(), args.src(),  args.pattern())
time.sleep(5)
move_dir(r"C:\Users\user\Downloads", r"C:\Users\user\Documents\pdf", r"*.pdf")

Creating a script to move files based on pattern in a specific directory. Initially ran into the raw string error when I hard coded and solved by making the output raw as suggested here : How to move a file in Python?

PROBLEM
As I tested with the hard code and had to put r before the string I wanted to test moving back from to the old directory and then recopying all over again. (Code was ran correctly once without the 2nd to last 2 lines)

I think I'm now failing to do that when calling the argument string as the error returned shows double backslashes '\'

C:\Users\user\Documents\GitHub\personalUtilities>python downloadOrganizer.py --src "C:\Users\user\Documents\pdf" --dst "C:\Users\user\Downloads" --pattern "*.pdf"  
usage: downloadOrganizer.py [-h] [--src SRC] [--dst DST] [--pattern PATTERN]
downloadOrganizer.py: error: argument --src: invalid path value: 'C:\\Users\\user\\Documents\\pdf'

I see a few articles on making a class to check if the directory exists but that wouldn't solve my problem if I'm passing the wrong string type and any article about this in the subject seems to be answering a different question or solving something else.

So whats the proper way to pass the source and destination directory strings from argparse?
Tried changing the type to path for src and dst but didn't change anything. tried adding r to the arg in the cli before the quote string that just made the directory extra wrong!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文