用 Python 建立創意項目交接用的本地檔案清單
Build a local file manifest for creative project handoffs in Python
這份 Python 教學提供一段本地檔案清單腳本,為創意項目檔案記錄相對路徑、位元組大小及 SHA-256 雜湊值。腳本以唯讀方式掃描檔案,略過符號連結並輸出排序後的 JSON,輸出檔應放在掃描資料夾以外。比對前後清單可偵測檔案內容是否改變,但相同雜湊不能證明內容正確、已獲授權、安全或批准。
A creative project can produce several small files that look almost identical: a source image, exports for different chat placements, a comparison grid, an outfit concept and a song draft. Before handing them off, it helps to know exactly which files you are sending and whether a file changed after review.
Disclosure: I build Emote Resizer, Free Image Merger, TryOnSwap and Songifted, which are mentioned as examples below. This article was drafted with AI assistance and is my own educational resource, not an independent review.
What a manifest can and cannot do
A manifest lists a file's relative path, byte length and SHA-256 digest. Comparing two manifests helps detect changes in file content; it does not prove the content is correct, licensed, safe, accessible or approved. A hash also does not hide a sensitive file name. Store and share the manifest only with people who may see the file names.
Example folder structure
creative-project/
sources/
original.png
exports/
emote-small.png
image-comparison.png
outfit-concept.png
music/
approved-lyrics.txt
preview-audio.mp3
Use only files that belong to the actual project; the names above are examples. Emote exports might come from Emote Resizer and a comparison image from Free Image Merger. A fashion concept can come from TryOnSwap, and a lyrics-and-audio gift from Songifted. These tools have different functions, preview terms and prices; a manifest does not change those terms. Keep private or unapproved images out of the folder you plan to share.
Generate an inventory locally
Save the following as manifest.py. It uses only the Python standard library and reads files without changing them. It skips symbolic links, reads in chunks, and prints sorted relative paths, byte sizes and hashes as JSON. Do not modify files while it runs; like any simple scan, it is not an atomic snapshot.
#!/usr/bin/env python3
"""Create a read-only SHA-256 inventory of regular files in a project folder."""
import hashlib
import json
import pathlib
import sys
def inventory(root: pathlib.Path) -> list[dict[str, object]]:
records = []
for path in sorted(root.rglob('*')):
if path.is_symlink() or not path.is_file():
continue
digest = hashlib.sha256()
with path.open('rb') as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b''):
digest.update(chunk)
records.append({'path': path.relative_to(root).as_posix(),
'bytes': path.stat().st_size, 'sha256': digest.hexdigest()})
return records
if __name__ == '__main__':
if len(sys.argv) != 2 or not pathlib.Path(sys.argv[1]).is_dir():
raise SystemExit('Usage: python3 manifest.py PROJECT_FOLDER')
print(json.dumps(inventory(pathlib.Path(sys.argv[1]).resolve()), indent=2))
Run it from a terminal, replacing the paths with your own:
python3 manifest.py /path/to/creative-project > /path/outside-project/manifest.json
Write the output outside the scanned folder. Shell redirection creates the output file before the script starts; placing it inside the folder can cause the script to include its own incomplete output. If a file cannot be read, the program stops with an error rather than silently omitting it. Correct the permission or remove the file from the handoff folder and run again.
Review and compare
- Open the files in the folder and check names, spelling, dimensions, consent, visual details and playback. Keep an approved reference copy.
- Generate a manifest after the review. If you later receive or copy the files, generate another manifest from the received folder and compare the
path,bytesandsha256values. Different hashes indicate different bytes; the same hash is a useful integrity check, not a substitute for reviewing content. - Keep the source and export separate. Avoid committing sensitive images, lyrics, manifests or private names to a public repository.
- Check the target service's current specifications and purchase conditions before promising a format or a free download. This sample script does not upload anything or alter product sites.
來源:DEV Community · dev.to