Dir.foreach(".") { |x| next if !File.directory?(x) next if x.to_i < 1 # and x.to_i > 1 lines = [] File.open(x + "/__init__.py", "r").each_line { |line| lines.push(line) } i = 0 while i < lines.length break if (lines[i].index("import") == 0) i += 1 end while i < lines.length i += 1 break unless (lines[i].index("from") == 0) end lines.insert(i, '', 'qn = "' + x + '"') File.open(x + "/__init__.py", "w") { |file| lines.each { |line| file.puts line } } puts x }