Python 文件组织脚本中的缩进和语法错误
我一直在研究教程中的文件组织脚本: 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 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
在左图中,您正在将代码输入到交互式 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 anif/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.