Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with text_processing/fix_headers/renameLN_LR.py #25

Open
slstaples opened this issue Aug 9, 2021 · 1 comment
Open

Issue with text_processing/fix_headers/renameLN_LR.py #25

slstaples opened this issue Aug 9, 2021 · 1 comment
Assignees

Comments

@slstaples
Copy link
Contributor

Hello! I've struggled with this a bit and need some help. I can't figure out why the program listed above won't run--Aleks indicated it should work, but it gives me the following error on a PC (I am doing essentially the same task, just changing GA to GR instead of LN to LR):
Traceback (most recent call last):
File "rename_GA_GR.py", line 16, in
with open(full_name, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test_files\GA\1\108_GA_1_NA_1_F_10539_UA.txt'

Thanks in advance for your help!

@markfullmer markfullmer self-assigned this Aug 9, 2021
@markfullmer
Copy link
Member

markfullmer commented Aug 9, 2021

@slstaples , I was able to reproduce the error you're seeing on a Mac.

The reason this is happening is that part of the script is still trying to process a filename which a previous part of the script has already renamed (i.e., the file changed 108_GA_1_NA_1_F_10539_UA.txt' to 108_GR_1_NA_1_F_10539_UA.txt', so, in effect the error is correct -- the file can no longer be found, and it therefore can't complete the operation of manipulating the header (<Assignment: GA>).

Looking at the code, this can be resolved by flipping the order of operations: first manipulate the header, then rename the file. I've shown the code change in a diff below, if that's useful for reference. This order of operations change should be made to the existing rename_LN_LR.py as well as to the new script you're working on, rename_GA_GR.py.

Let me know if you have any additional questions!

diff --git a/fix_headers/renameLN_LR.py b/fix_headers/renameLN_LR.py
index a4a44bc..a728fbc 100755
--- a/fix_headers/renameLN_LR.py
+++ b/fix_headers/renameLN_LR.py
@@ -10,8 +10,6 @@ args = parser.parse_args()
 for root, dirs, files in os.walk(args.dir):
     for fileName in files:
         full_name= os.path.join(root,fileName)
-        if "_LN_" in full_name:
-            os.rename(full_name, full_name.replace("_LN_", "_LR_"))
         if ".txt" in full_name:
             with open(full_name, 'r') as f:
                 s= f.read()
@@ -20,5 +18,6 @@ for root, dirs, files in os.walk(args.dir):
                    with open(full_name, "w") as f:
                             f.write(s)
                             f.close()
-
-
+        if "_LN_" in full_name:
+            os.rename(full_name, full_name.replace("_LN_", "_LR_"))
+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants