Googleフォームで投稿されたファイルには、元のファイルだけでなく、学生の名前が自動的に付く仕様になっています(for Educationなどでログイン指定をした場合)。これは、はっきりいって邪魔なので削除するPythonスクリプトです。なお、ファイルはダウンロードしたうえでローカルで処理するアプローチです。Googleドライブ上でやってしまう場合は、GASかな。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removeStudentNameInFile(directory): | |
""" | |
Googleフォームで投稿されたファイルから学生の名前を削除する | |
Args: | |
directory (str): ファイルが保存されているディレクトリのパス | |
""" | |
# ディレクトリ内のすべてのファイルを処理 | |
for filename in os.listdir(directory): | |
# " - "の位置を検索 | |
pos = filename.find(" - ") | |
if pos != -1: | |
# 新しいファイル名を生成 | |
new_filename = filename[:pos] + filename[filename.rfind("."):] | |
# ファイル名を変更 | |
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename)) | |
print(f'Renamed: {filename} to {new_filename}') |