LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error in python code. (https://www.linuxquestions.org/questions/programming-9/error-in-python-code-4175735769/)

ajiten 04-09-2024 09:38 AM

Quote:

Originally Posted by michaelk (Post 6494899)
Code:

import os
from natsort import natsorted

for file in os.listdir():
    print(file)

sorted_files=natsorted(os.listdir())
print(sorted_files)

files from output of ls:

program output:
Code:

1.2.png
screenshot (1573).png
1.3.png
1.21.png
1.10.png
1.1.png
1.11.png

['1.1.png', '1.2.png', '1.3.png', '1.10.png', '1.11.png', '1.21.png', 'screenshot (1573).png']


Modified the program, to the one below:
Code:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  7 17:49:51 2024

@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
from natsort import natsorted
path = os.chdir(input_dir)

j=1
for file in os.listdir(path):
    print(j,':', file)
    j+=1
   
j=1   
sorted_files = natsorted(os.listdir(path))
for file in sorted_files:
    print('>', j, ':', file)
   
i=1
for file in sorted_files: #os.listdir(path):
    filetype=file.split('.')[-1]
    print(input_dir+ '/'+file)
    os.rename(input_dir+ '/'+file, input_dir+ '/'+'1.'+str(i)+'.'+filetype)
    i +=1
    print (file)

But, this gave the error:
Code:

runfile('C:/Users/HP/a.py', wdir='C:/Users/HP')
Traceback (most recent call last):

  File D:\Program Files\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\hp\a.py:9
    from natsort import natsorted

ModuleNotFoundError: No module named 'natsort'

So, natsort need be installed on spyder.

Seems there is no way to install natsort on spyder, as got this impression after extensive googling.

On restarting kernel, and applying other means, finally got to the below command:, which also didn't work:
Code:

install natsort
  Cell In[5], line 1
    install natsort
            ^
SyntaxError: invalid syntax

Hence, tried to run the above program on jupyter notebook, but that too gives error:
Code:

---------------------------------------------------------------------------
ModuleNotFoundError                      Traceback (most recent call last)
Cell In[1], line 2
      1 import os
----> 2 from natsort import natsorted

ModuleNotFoundError: No module named 'natsort'

Tried googling for installing natsort, on Anaconda, and got the results on googling.

But, even these results seem not to help.

michaelk 04-09-2024 09:55 AM

You might be able to install it via pip but I don't know if spyder will automatically know it exists. Regardless of sorting your original code was just renaming files without really knowing what you were actually renaming. From the code it seems like all you want to do is replace the first "." with "_". What don't you use string replace and search for a "1." (to pickup the first "." and replace it with a "1_"

ajiten 04-09-2024 06:53 PM

Quote:

Originally Posted by michaelk (Post 6494997)
You might be able to install it via pip but I don't know if spyder will automatically know it exists. Regardless of sorting your original code was just renaming files without really knowing what you were actually renaming. From the code it seems like all you want to do is replace the first "." with "_". What don't you use string replace and search for a "1." (to pickup the first "." and replace it with a "1_"

But, again the files are unsorted, and the same problem should occur in the part after the '_'.

Please tell if should make a separate post about the installation of natsort, on Anaconda?

michaelk 04-09-2024 07:38 PM

Yes, you can create a new thread on natsort module.

However, looking at it from a new angle..
Code:

import os

for file in os.listdir():
    x = file.split(".")
    print("old file name:",file)
    size=len(x)
    if size == 3:
      new_file=x[0]+"_"+x[1]+"."+x[2]
      print("new file name:",new_file,"\n")
    else:
      print("new file name: No Change\n")

files:
Quote:

1.10.png 1.1.png 1.2.png 1_34.png 'screenshot (1573).png'
1.11.png 1.21.png 1.3.png
output:
Code:

old file name:  1.2.png
new file name:  1_2.png

old file name:  screenshot (1573).png
new file name: No Change

old file name:  1.3.png
new file name:  1_3.png

old file name:  1.21.png
new file name:  1_21.png

old file name:  1.10.png
new file name:  1_10.png

old file name:  1_34.png
new file name: No Change

old file name:  1.1.png
new file name:  1_1.png

old file name:  1.11.png
new file name:  1_11.png

All based on the previously posted filenames and my assumptions on what you are trying to accomplish.

dugan 04-11-2024 08:48 AM

Look. Honestly, you should solve this by renaming the images. You need to zero-pad the numbers. Assuming the numbers go into the dozens, 1_1.png would need to be renamed to 1_01.png. If they go into the hundreds, then it becomes 1_001.png.

If you can’t just save out another image sequence with the proper padded numbering, then you can use Python to do the renaming. I can give you help with that, if you need it.


All times are GMT -5. The time now is 11:28 AM.