0

Silly question, but I want to do some processing on a dataset and put them into different CSVs, like UDID1.csv, UDID2.csv, ..., UDID1000.csv. So this is my code:

for i in 1..1000
    logfile = File.new('C:\Users\hp1\Desktop\Datasets\New File\UDID#{i}\.csv',"a")
    #I'll do some processing here
end

But the program throws an error when running because of the UDID#{i} part. So, how to overcome this issue? Thanks.

Edit: This is the error:

in `initialize': No such file or directory @ rb_sysopen - C:\Users\hp1\Desktop\Datasets\New File\udid#{1}\.csv (Errno::ENOENT)from C:/Ruby21/bin/hashedUDID.rb:38:in `new' from C:/Ruby21/bin/hashedUDID.rb:38:in '<main>'
0

3 Answers 3

2

The ' is one problem, another problem is the path.

In your posting the New File must exist as a directory. Inside this directory must exist another directories like UDID0001. This gets a .csv file.

Correct is (I don't use the non-rubyesk for-loop):

1.upto(1000) do |i|
    logfile = File.new("C:\\Users\\hp1\\Desktop\\Datasets\\UDID#{i}.csv", "a")
    #I'll do some processing here
    logfile.close #Don't forget to close the file
end

Inside " the backslash must be masked (\\). Instead you may use /:

logfile = File.new("C:/Users/hp1/Desktop/Datasets/New File/UDID#{i}/.csv", "a")

Another possibility is the usage of %i to insert the number:

    logfile = File.new("C:/Users/hp1/Desktop/Datasets/New File/UDID%02i/.csv" % i, "a")

I prefer to use open, then the file is closed with the end of the block:

    File.open("C:/Users/hp1/Desktop/Datasets/New File/UDID%04i/.csv" % i, "a") do |logfile|
      #I'll do some processing here
    end #closes the file

Warning:

I'm not sure, if you really want to create 1000 log files (The File is opened inside the loop. so each step creates a file.). If yes, then the %04i-version has the advantage, that the files get all the same number of digits (starting with 0001 and ending with 1000).

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, that worked! Thanks! As for why I need this, actually my original problem can be found here: stackoverflow.com/questions/32797346/… So, because the hashed array can't store so many values, I thought of partitioning my dataset into smaller manageable chunks, hashing them, storing them in separate CSV files (which is this question), & then compare them with subsequent hashings.
If this answer solves the problem of your question you should accept the answer.
for a thorough solution +1
0
(1..10).each { |i| logfile = File.new("/base/path/UDID#{i}.csv") }

You must use double quote (") when you need string interpolation.

1 Comment

how does this add to my existing answer?
0

#{} can only be used in strings with double quotes ". So change your code to:

for i in 1..1000
  logfile = File.new("C:\Users\hp1\Desktop\Datasets\New File\UDID#{i}\.csv","a")
  # other stuff
end

1 Comment

Except that #{} is not a constructor :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.