I have a list of xml files in a directory for example:
file1.xml
file2.xml
file3.xml
file4.xml
file5.xml
file10.xml
file11.xml
file12.xml
file22.xml
file23.xml
file24.xml
file31.xml
file32.xml
file33.xml
When I use os.listdir(path) and print the file names, the output is as follows:
file1.xml
file10.xml
file11.xml
file12.xml
file2.xml
file22.xml
file23.xml
file24.xml
file3.xml
file31.xml
file32.xml
file33.xml
file4.xml
file5.xml
Expected Outptut
file1.xml
file2.xml
file3.xml
file4.xml
file5.xml
file10.xml
file11.xml
file12.xml
file22.xml
file23.xml
file24.xml
file31.xml
file32.xml
file33.xml
Can any1 tell me if there is a solution for this problem. Thanks in advance!!
As per the docs for os.listdir (emphasis mine):
os.listdir(path)
Return a list containing the names of the entries in the directory
given by path. The list is in arbitrary order. It does not include the
special entries '.' and '..' even if they are present in the
directory.
Given your example, perhaps the easiest way is to sort the list by extracting the numbers from the name, and sorting by those:
import os, re
filenames = os.listdir('/path/to/your/files')
filenames.sort(key=lambda L: map(int, re.findall('\d+', L)))
Related
I have a text file that has around 100 plus entries like out.txt:
domain\1esrt
domain\2345p
yrtfj
tkpdp
....
....
I have to read out.txt, line-by-line and check whether the strings like "domain\1esrt" are present in any of the files under a different directory. If present delete only that string occurrence and save the file.
I know how to read a file line-by-line and also know how to grep for a string in multiple files in a directory but I'm not sure how to join those two to achieve my above requirement.
You can create an array with all the words or strings you want to find and then delete/replace:
strings_to_delete = ['aaa', 'domain\1esrt', 'delete_me']
Then to read the file and use map to create an array with all the lines who doesn't match with none of the elements in the array created before:
# read the file 'text.txt'
lines = File.open('text.txt', 'r').map do|line|
# unless the line matches with some value on the strings_to_delete array
line unless strings_to_delete.any? do |word|
word == line.strip
end
# then remove the nil elements
end.reject(&:nil?)
And then open the file again but this time to write on it, all the lines which didn't match with the values in the strings_to_delete array:
File.open('text.txt', 'w') do |line|
lines.each do |element|
line.write element
end
end
The txt file looks like:
aaa
domain\1esrt
domain\2345p
yrtfj
tkpdp
....
....
delete_me
I don't know how it'll work with a bigger file, anyways, I hope it helps.
I would suggest using gsub here. It will run a regex search on the string and replace it with the second parameter. So if you only have to replace any single string, I believe you can simply run gsub on that string (including the newline) and replace it with an empty string:
new_file_text = text.gsub(/regex_string\n/, "")
I have a set of images located in a folder and I'm trying to read these images and store their names in text file. Where the order of images is very important.
My code as follow:
imagefiles = dir('*jpg');
nfiles = length(imagefiles); % Number of files found
%*******************
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
The images stored in the folder in the following sequence : {1,2,3,4,100,110}.
The problem that Matlab read and write the sequence of images as { 1,100,110,2,3,4}. Which is not the correct order.
How can this be overcome?
I would suggest to use scanf to find the number of the file. For that you have to create a format spec which shows how your file name is built. If it is a number, followed by .jpg, that would be: '%d.jpg'.
You can call sscanf (scan string) on the name's of the files using cellfun:
imagefiles = dir('*jpg');
fileNo = cellfun(#(x)sscanf(x,'%d.jpg'),{imagefiles(:).name});
Then you sort fileNo, save the indexes of the sorted array and go through these indexes in the for-loop:
[~,ind] = sort(fileNo);
for ii=ind
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
Longtime lurker, first time posting! I'm new to Ruby so I would love some help on this.
I have a large text file with a list of files separated by a break, so it looks like this:
ARO_9501.jpg
ARO_9506.jpg
IMG_1499.jpg
IMG_1511.jpg
How can I get this text file into an array so I can call .each on it and copy the files to another directory?
This is how I generally do:
fileNameArray = File.read("/path/to/file.txt").split("\n")
Or, if you just need to iterate over the file names and don't necessarily need an array containing the names (it looks like you don't), I usually use this:
File.read("/path/to/file.txt").each_line do |line|
# do operations using line
end
Docs:
IO::read (File extends IO)
String .split() and each_line()
You can go this way also using IO::readlines :
ar = File.open("/home/kirti/ruby/foo.txt","r") do |fil|
fil.readlines.map(&:strip)
end
p ar
# >> ["ARO_9501.jpg", "ARO_9506.jpg", "IMG_1499.jpg", "IMG_1511.jpg"]
As per the #steenslag comments:
ar = File.readlines("/home/kirti/ruby/foo.txt").map(&:chomp)
ar # => [ "ARO_9501.jpg", "ARO_9506.jpg", "IMG_1499.jpg", "IMG_1511.jpg"]
I am writing a function for remote sensing purposes using matlab
the user will enter a folder containing 7 files into the program each file is a band of an image and the names of them is:
"b1.dat"
"b2.dat"
"b3.dat"
"b4.dat"
"b5.dat"
"b6.dat"
"b7.dat"
for example if 2 is entered as the argument of the function it will search in seven file names that are in the access and then will show b2.dat
how do you suggest me to write the code
You can use uigetfiles to select the directory and dir to get a list of the folders contents. Once you have the list, strfind will tell you a file contains a given number.
Or, using uigetdir:
dirName = uigetdir('C:\', 'select a directory');
contents = dir(dirName);
for c = contents
name = c.name;
if strfind(name,'3')
fileToOpen = name{1};
end
end
I used these two lines of codes:
folder = uigetdir('D:\','Select the folder containing bands')
filenames = dir(folder)
the first line returns the path to the folder as I expected:
folder =
D:\RS\911130 TM bands
but the second line not. I have 7 files in my folder and it returns a 9x1 struct
filenames =
9x1 struct array with fields:
name
date
bytes
isdir
datenum
for example the contents of the filenames(1,1) is:
I am attempting to find a file by its name within a directory. I am not sure what the best approach to this problem is. The file could be nested in other directories within the root directory.
You can use Dir.glob, for example:
Dir.glob(File.join("**","*.rb"))
It will recursively look for "*.rb" files in your current directory.
You could use Dir.glob or Dir[]:
Dir['the_directory/**/the_filename']
The ** matches 0 or more directories recursively. It returns an array of filenames that match.
this should work for you:
require 'find'
file_name = /log\Z/
path = './'
found_files = Find.find(path).inject([]) do |files, entry|
File.file?(entry) && File.basename(entry) =~ file_name ?
files << entry : files
end
p found_files
#=> ["./Maildir/dovecot.index.log", "./pgadmin.log"]
change file_name and path to your needs.