LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2024, 09:38 AM   #16
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4

Quote:
Originally Posted by michaelk View Post
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.

Last edited by ajiten; 04-09-2024 at 09:43 AM.
 
Old 04-09-2024, 09:55 AM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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_"
 
Old 04-09-2024, 06:53 PM   #18
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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?
 
Old 04-09-2024, 07:38 PM   #19
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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.
 
Old 04-11-2024, 08:48 AM   #20
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Converting your Python 2 code to Python 3 LXer Syndicated Linux News 0 03-28-2020 10:03 PM
[SOLVED] NEED HELP! Want to code PERL and Python on VS Code on Fedora KDE 27 GHOSTIN7HESHELL Linux - Software 13 02-15-2018 05:00 PM
[SOLVED] Learning Python, how should I implement the following problem in Python 3 code? rblampain Programming 8 05-29-2016 10:53 AM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration