开发者

Randomize Multiple Text File and Compile into One

开发者 https://www.devze.com 2023-04-13 03:56 出处:网络
So, a little 开发者_如何转开发idea I\'m working on and I\'m missing one piece of it. I\'m not sure what language of coding to go about this in but I\'m sure it can be done it most, so any with experie

So, a little 开发者_如何转开发idea I'm working on and I'm missing one piece of it. I'm not sure what language of coding to go about this in but I'm sure it can be done it most, so any with experience in your favourite language, I could really use your help!

In an example, lets say I have multiple text files (hundreads of them!), each text file has multiple lines. What I'm looking to do is when I run program/script, it'll compile all the text files in to one text file, but in a random order each time.

Google didn't really give me anything solid on this idea. So any help is greatly appreciated!

EDIT:

Problem solved thanks to the help of code-monkey. This is his work with a bit of an alter to it to do what I wanted. Thanks everyone for the help. :)

import os
import random

os.chdir("C:/Users/USERACCOUNT/test/txtfiles")
directory = "C:/Users/USERACCOUNT/test/txtfiles"
extension = ".txt"
files = [file for file in os.listdir(directory) if file.lower().endswith(extension)]

file = open("C:/Users/USERACCOUNT/test/result.txt", "a")

random.shuffle(files)

for x in files:

    f = open(os.getcwd() + "\\" + x, "r")
    res = f.read()
    file.write(res + "\r\n")

f.close()
file.close()


i really like python for things like this, it's very easy.

here is an example:

import os
import random

files = os.listdir("C:\\Your\\Directory\\")

file = open("result.txt", "a")

random.shuffle(files)

for x in files:

    f = open(os.getcwd() + "\\" + x, "r")
    res = f.read()
    file.write(res + "\r\n")

f.close()
file.close()

hope that helps!


something like this.. c#

static void ManyFilesToOne() {
        string dir = @"c:\miscworking\myfiles";
        FileInfo[] files = new DirectoryInfo(dir).GetFiles("*.txt");
        List<int> usedIdx = new List<int>();

        StringBuilder str = new StringBuilder();
        Random rnd = new Random();
        while (usedIdx.Count < files.Length) {

            int idx = rnd.Next(0, files.Length);
            while (usedIdx.FindIndex(delegate(int i) { return i == idx; }) >= 0) {
                idx = rnd.Next(0, files.Length);
                                }

            usedIdx.Add(idx);
            str.Append(File.ReadAllText(files[idx].FullName));
        }

        File.WriteAllText(Path.Combine(dir, @"File\BigFile" + rnd.Next(10000) + ".txt"), str.ToString() + System.Environment.NewLine);
    }


In Perl:

#!/usr/bin/env perl

use autodie;
use strict;
use warnings;

use File::Find;
use File::Slurp;
use List::Util qw( shuffle );

my $path = shift or die "Usage: $0 PATH\n";
open my $fh_output, '>', 'compiled.txt';

my @filenames;

sub compile {
    push @filenames, $File::Find::name unless -d;
}
find( \&compile, $path );

for my $filename ( shuffle @filenames ) {
    my $content = read_file $filename;
    print $fh_output $content if defined $content;
}

close $fh_output;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号