Python 文件组织脚本中的缩进和语法错误

发布于 2025-01-13 15:52:59 字数 1152 浏览 3 评论 0原文

我一直在研究教程中的文件组织脚本: https:// www.youtube.com/watch?v=MRuq3SRXses&t=22s

但是,我是 Python 新手,所以它的很多细微差别对我来说都是隐藏的。

输入图片这里的描述

我有两个缩进错误,以及两个语法错误,但是在查看了这里的几个问题后,我似乎仍然无法修复它。 我确信我没有混合空格和制表符,而且我使用的教程中的人似乎使用他的脚本版本非常好。

至于语法错误,我不知道问题出在哪里。

这是我的代码的副本:

import os
import shutil

origin_dir = r'D:\TTITF\Game Files and Animations\Animations\Bridges\Walk ~ Alvie - V_2 R RAW'
target_dir = r'D:\TTITF\Game Files and Animations\Animations\Bridges\Walk ~ Alvie - V_2 R TRIMMED'



for f in os.listdir(origin_dir):
    filename, file_ext = os.path.splitext(f)

    try:
        if not file_ext:
            pass

        elif int(filename) in range(0, 60):
            shutil.move(
                os.path.join(origin_dir, f'{filename}{file_ext}')
                os.path.join(target_dir, 'V_O to V_4', f'{filename}{file_ext}'))


    except (FileNotFoundError, PermissionError):
        pass

I've been working on a file organizing script from a tutorial: https://www.youtube.com/watch?v=MRuq3SRXses&t=22s

However, I'm new to python, so much of its nuance is hidden to me.

enter image description here

I have two indentation errors, as well as two syntax errors, however after looking at several questions on here, I still can't seem to fix it.
I'm confident I did not mix spaces and tabs, and the person from the tutorial I used seem to use his version of the script perfectly fine.

As for the syntax errors, I do not know what the issue could be.

Here is a copy of my code:

import os
import shutil

origin_dir = r'D:\TTITF\Game Files and Animations\Animations\Bridges\Walk ~ Alvie - V_2 R RAW'
target_dir = r'D:\TTITF\Game Files and Animations\Animations\Bridges\Walk ~ Alvie - V_2 R TRIMMED'



for f in os.listdir(origin_dir):
    filename, file_ext = os.path.splitext(f)

    try:
        if not file_ext:
            pass

        elif int(filename) in range(0, 60):
            shutil.move(
                os.path.join(origin_dir, f'{filename}{file_ext}')
                os.path.join(target_dir, 'V_O to V_4', f'{filename}{file_ext}'))


    except (FileNotFoundError, PermissionError):
        pass

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

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

发布评论

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

评论(2

离旧人 2025-01-20 15:52:59

您需要在 IDE 上运行 python 代码,例如 VS code、Jupyter Notebook、Pycharm(您正在观看的教程中提到)。 代码

大多数开发人员在终端中编写

时,您需要键入 keep 并留出空间,记住代码所需的缩进在终端中编写的有用命令

按 Enter ->;输入新行
双击回车->执行代码

You need to run the python code on IDE such as VS code,Jupyter notebook, Pycharm ( which is mentioned in tutorial that you are watching). Most developer write code in

When you are typing in the terminal you need to type keep and give space keeping in mind the indentation needed for the code

Useful commands to write in terminal

Press enter -> To enter a new line
Double press enter -> To execute the code

沧笙踏歌 2025-01-20 15:52:59

在左图中,您正在将代码输入到交互式 shell 中,该 shell 有一些限制。特别是,如果您要输入多行结构,例如 try/ except 块或 if/else 块,则不能使用任何空行,否则解释器认为该块已经完成。

在右图中,您在命令提示符下输入代码,这是错误的。

In the image on the left, you're typing your code into an interactive shell, which has some limitations. In particular, if you're entering a multiline construct such as a try/except block or an if/else block, you can't use any blank lines, otherwise the interpreter thinks the block is finished.

In the image on the right, you're typing your code at a command prompt, which is just wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文