Commit 2a3125223424f4ecd2ce5109c39f17d95a7552e1

Authored by Odile
Committed by GitHub
2 parents 0d6fbe51 bb3082d8
Exists in master

Merge pull request #3 from odile9999/issue2

Fix #2. Check prefix before add or remove prefix.
Showing 1 changed file with 11 additions and 5 deletions   Show diff stats
src/files.py
... ... @@ -36,8 +36,11 @@ class PireneaFiles(object):
36 36 prefix += "_"
37 37 for path, _dirs, files in os.walk(self.folder):
38 38 for filename in files:
39   - new_filename = prefix + filename
40   - os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
  39 + if filename.startswith("P"):
  40 + print("File %s has already a prefix.")
  41 + else:
  42 + new_filename = prefix + filename
  43 + os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
41 44  
42 45 def remove_prefix(self, prefix="P0"):
43 46 """
... ... @@ -47,9 +50,12 @@ class PireneaFiles(object):
47 50 prefix += "_"
48 51 for path, _dirs, files in os.walk(self.folder):
49 52 for filename in files:
50   - f = filename.split(prefix)
51   - new_filename = prefix.join(f[1:])
52   - os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
  53 + if filename.startswith(prefix):
  54 + f = filename.split(prefix)
  55 + new_filename = prefix.join(f[1:])
  56 + os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
  57 + else:
  58 + print("File {0} does not begin with prefix : {1}".format(filename, prefix[:2]))
53 59  
54 60 def __check_prefix(self, prefix="P0"):
55 61 """
... ...