Search This Blog

Friday, October 27, 2017

New automated sorting program to be made

So I found that have to do much manual jobs when I download links from google to finds and rank best valuabale links to go to TOP position. I made research and found:
1) It is possible to iterate through folders. I already use these programs using os path and os walk;

2) It is possible to select nth file in a folder. I found code on stackoverflow:
Search "Python: Select the first file in a directory"
os.listdir(path)[0]
 import os fnames
fnames = os.listdir(path)def foo(path): fnames[0]

3) I found similar program which I have to adapt to make my program:

Python Script to iterate through folder and copy all feature classes in MXDs to new file geodatabases?
The code link:


On stackoverflow I found another useful code:
Start processing files from N-th file in folder. 
Slice notation can be used in this case:
for f in glob.glob(...)[n:]: with open(f) as inputfile:
 So it will take time to read code to make for automatio the programa whicg you can use to select and copy or move nth files in folder. I already use similar porgram selecting document types. Code style shall be implemented into blogger.

So I tried to adapt code but with failure of shutil. I think I will have to make copying with os library or other way.

# python OSWALKSHUTIL.py
# cd C:\Users\ANTRAS\Desktop

import os
import shutil

src = r'C:\\Users\\ANTRAS\\Documents\\LINKING\\1'
dest = r'C:\\Users\\ANTRAS\\Documents\\1'

for path, subdirs, files in os.walk(src):
    for name in files:
        name = os.listdir(path)[0]
        print name
shutil.copy2(name, dest)


Error:
C:\Users\ANTRAS\Desktop>python OSWALKSHUTIL.py
www.google.co.uk_25th_Oct_2017 (1).csv
Traceback (most recent call last):
  File "OSWALKSHUTIL.py", line 14, in <module>
    shutil.copy2(name, dest)
  File "C:\Python27\lib\shutil.py", line 144, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 96, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'www.google.co.uk_25th_Oct_2017 (1).csv'


I found very useful shutil tutorial with example how to automate. There is no in library possibilities to automate generally with path file index.
https://pymotw.com/3/shutil/
There is also simpe solution - with os library enumerate files and the using file name part, 1,2 and etc.. use os.walk method to copy all files to new folder.

Similar solution to rename file in folder 
Python script to rename files in directory, transforming spaces to hyphens and the chars to lowercase
https://gist.github.com/igniteflow/1226919


import os
"""
Renames the filenames within the same directory to be Unix friendly
(1) Changes spaces to hyphens
(2) Makes lowercase (not a Unix requirement, just looks better ;)
Usage:
python rename.py
"""
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:


os.rename(filename, filename.replace(" ", "-").lower())


Python script to automatically rename scene filenames to a more user-friendly format.

scenerename In github this projec tcan be used to rename files or code can be adapted for own need. 

Idea from stackoverflow how to iterate files in folder.

How to copy first 100 files from a directory of thousands of files using python?


You can always make an iterator in advance and then call the next() method to get the next file:

i = listdir(".")
while True:
    try: name = i.next()
    except StopIteration: sleep(1)
# This probably won't work as imagined though

Aditional great resource to automate with os library Python learn
https://www.pythonlearn.com/html-008/cfbook017.html


So such tools how to automate files will be very useful. I will program the program (or will copy necessary codes) to rename files in folders. The file system is useful to organize and to extract or copy instantly necessary files to one folder. Filename renaming will be also be done variable date, numeration, alphabet or variable additional arguments.
Also program to make action with files in folder in sequence without knowing names and types. 
So plan is to:
1. To test existing project;
2. os library methods and examples;
3. Iteration methods with multiples files;
4. Next method and examples;
5. Shutil integration;
6. Other methods to loop and copy variables.

It will help to automate and integrate mutlple tasks and codes.

For a while I can proceed with code, which I already use in programs.
# python EXTRACT.py
# cd C:\Users\ANTRAS\Desktop

import os
import shutil

src = r'C:\\Users\\ANTRAS\\Documents\\LINKING\\1'
dest = r'C:\\Users\\ANTRAS\\Documents\\1'

for path, subdirs, files in os.walk(src):
    for name in files:
        filenamew = os.path.join(path, name)
        if '(1) .csv' in filenamew:
            shutil.copy2(filenamew, dest)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.