11
\$\begingroup\$

Challenge

Create a new file and write the string Hello World to it.

Restrictions

  • Your challenge must write to a file on disk, in the file system.

  • The file may not be a log file generated during normal operation of the interpreter.

  • The file must contain only the string Hello World. It is allowed to contain a trailing newline or minimal whitespace. No other content.

  • No command-line flags/pipes (etc) allowed, except when necessary to run the program. (e.g. perl -p)

Notes

  • This is , so shortest program in bytes wins.

  • Follow the spirit, not the letter, of the rules.

\$\endgroup\$
  • \$\begingroup\$ Is trailing newline okay? \$\endgroup\$ – Winny Aug 1 '16 at 9:03
  • \$\begingroup\$ @Winny yes, it is ok \$\endgroup\$ – dkudriavtsev Aug 1 '16 at 20:23
  • \$\begingroup\$ Is a filename part of the contents of a file? \$\endgroup\$ – CousinCocaine Aug 12 '16 at 20:36

50 Answers 50

1
\$\begingroup\$

Pyth, 14 bytes

.w"Hello World

Outputs to a file called o.txt.

\$\endgroup\$
24
\$\begingroup\$

Python 2, 32 bytes

print>>open(*"ww"),"Hello World"

Yes, this is valid python.

\$\endgroup\$
  • 1
    \$\begingroup\$ Woah. Explanation? \$\endgroup\$ – DJMcMayhem Aug 1 '16 at 6:22
  • 1
    \$\begingroup\$ @DrGreenEggsandIronMan it looks like the main trick is how * can split an iterable into individual arguments in a function, so open(*"ww") becomes open("w","w"). \$\endgroup\$ – Value Ink Aug 1 '16 at 9:09
  • 7
    \$\begingroup\$ @ValueInk Actually the main trick is the use of the extended print statement which is rarely seen in the wild. \$\endgroup\$ – xsot Aug 1 '16 at 10:20
  • 1
    \$\begingroup\$ That is indeed a cool trick, but if it weren't for your *"ww" trick you'd've tied with the other Python answer here, which uses open(...).write(...) instead for the same byte count \$\endgroup\$ – Value Ink Aug 1 '16 at 10:23
  • \$\begingroup\$ Oops, I didn't even realise that. \$\endgroup\$ – xsot Aug 1 '16 at 10:26
8
\$\begingroup\$

Haskell, 25 bytes

writeFile"o""Hello World"
\$\endgroup\$
6
\$\begingroup\$

zsh, 17 bytes

<<<Hello\ World>x

Outputs to a file called x.

\$\endgroup\$
  • \$\begingroup\$ Can you interpreted the > as a pipe? \$\endgroup\$ – CousinCocaine Aug 12 '16 at 20:34
  • \$\begingroup\$ What about >Hello\ World \$\endgroup\$ – CousinCocaine Aug 12 '16 at 20:41
5
\$\begingroup\$

Ruby, 26 bytes

Writes to file f.

open(?f,?w)<<"Hello World"
\$\endgroup\$
5
\$\begingroup\$

Batch, 18 bytes

echo Hello World>f
\$\endgroup\$
  • \$\begingroup\$ I think you need an @, or re-title this as (console). \$\endgroup\$ – Erik the Outgolfer Oct 2 '16 at 18:13
  • \$\begingroup\$ The @ is not necessary. \$\endgroup\$ – Shaun Wild Oct 3 '16 at 8:07
  • \$\begingroup\$ Try saving it in a batch file and run it as such (not directly type the command). You will then see that you need the @, because the command is otherwise printed too. That means you have two options: 1) Delete this answer because it will be a dupe 2) Relabel as (console). \$\endgroup\$ – Erik the Outgolfer Oct 3 '16 at 10:35
5
\$\begingroup\$

Vim, 15 + 2 == 17 bytes

iHello World<esc>ZZ

+2 bytes for launching this with vim f instead of vim. Additionally, this version works to:

iHello World<C-o>ZZ

If launching vim like this is not allowed, there is also:

Vim, 18 bytes

iHello World<esc>:w f<cr>

Side note: this is a polyglot. The same thing works in V, except that it is one byte shorter (since the <cr> at the end is implicit.)

\$\endgroup\$
  • \$\begingroup\$ What's the ` f<cr>` at the end for? \$\endgroup\$ – Zwei Aug 1 '16 at 6:05
  • \$\begingroup\$ @Zwei 'f' is the name of the file, and the <cr> (which is a mnemonic for "enter", a single byte) is necessary to run the command. Commands that start with a colon are like a mini shell inside of vim, and the command :w is the command for writing to a file. \$\endgroup\$ – DJMcMayhem Aug 1 '16 at 6:08
  • \$\begingroup\$ Should <esc> not be counted as 5 bytes and <cr> as 4, since they have to be fully typed in? \$\endgroup\$ – Bart van Nierop Aug 3 '16 at 5:38
  • \$\begingroup\$ @BartvanNierop No, <esc> is just notation for "The escape key", which is 0x1B, and <cr> is notation for "The Carriage Return key" which is 0x0B \$\endgroup\$ – DJMcMayhem Aug 3 '16 at 5:39
  • 1
    \$\begingroup\$ Well first off, there is a precedent to score vim with keystrokes == bytes, but second off, there are several ways to write this in vim "code" without using the vim-key notation. For example, if you use <C-v> to insert a literal escape character and a literal carriage return, then you can assign all of these strokes to a macro, and run it that way. You could also do it in vimscript with normal 'iHello World^[:w f^M which is how vim displays it, not how you enter it. ^[ and ^M are both one byte. \$\endgroup\$ – DJMcMayhem Aug 3 '16 at 5:50
4
\$\begingroup\$

Batch, 19 bytes

@echo Hello World>o
\$\endgroup\$
  • \$\begingroup\$ Not sure if it counts as wrong, but it outputs "Hello World" and a newline to the file, rather than simply "Hello World" \$\endgroup\$ – brianush1 Aug 1 '16 at 12:18
  • \$\begingroup\$ What is the '@' good for? \$\endgroup\$ – PEAR Aug 1 '16 at 13:03
  • \$\begingroup\$ @PEAR It prevents the command from being echoed to STDOUT. \$\endgroup\$ – Neil Aug 1 '16 at 13:18
  • \$\begingroup\$ @brianush1 It doesn't count as wrong, apparently. \$\endgroup\$ – Erik the Outgolfer Oct 2 '16 at 18:14
3
\$\begingroup\$

C, 44 bytes

main(){fputs("Hello World",fopen("o","w"));}
\$\endgroup\$
  • 1
    \$\begingroup\$ This will segfault on some systems without #include <stdio.h> \$\endgroup\$ – Steven Penny Aug 1 '16 at 12:23
  • \$\begingroup\$ @StevenPenny As long as there's a system/compiler where it's guaranteed not to segfault, that's fine. Answers generally don't have to be portable. \$\endgroup\$ – Martin Ender Oct 2 '16 at 20:57
  • \$\begingroup\$ @StevenPenny And now it has. \$\endgroup\$ – orlp Oct 3 '16 at 5:54
3
\$\begingroup\$

PowerShell, 15 bytes

"Hello World">o

> redirects the string to a file called o in the current directory.

\$\endgroup\$
2
\$\begingroup\$

Dyalog APL, 19 bytes

⎕NPUT⍨'Hello World'

Creates a file with the name and contents "Hello World".

\$\endgroup\$
2
\$\begingroup\$

ed, 19 characters

i
Hello World
.
w o

Sample run:

bash-4.3$ ed <<< $'i\nHello World\n.\nw o'
12

bash-4.3$ cat o
Hello World
\$\endgroup\$
2
\$\begingroup\$

MATL, 15 bytes

'Hello World'Z#

This creates a file called inout and writes the string to it.

\$\endgroup\$
2
\$\begingroup\$

K, 20 Bytes

    `:f 0:,"Hello World"
    `:f

Confirmation;

    mmm@chromozorz:~/q$ cat f.txt 
    Hello World
\$\endgroup\$
  • \$\begingroup\$ Since nobody said it yet, welcome to Programming Puzzles & Code Golf. \$\endgroup\$ – Erik the Outgolfer Oct 2 '16 at 18:19
  • \$\begingroup\$ Much appreciated! \$\endgroup\$ – Chromozorz Oct 5 '16 at 19:54
  • \$\begingroup\$ OK. I also think that you have extra leading spaces though. I will edit the spaces out if you want (I don't know if they are intentionally placed, but I think it was unintentional.) \$\endgroup\$ – Erik the Outgolfer Oct 6 '16 at 5:22
2
\$\begingroup\$

Clojure, 23 bytes

#(spit"x""Hello World")

Anonymous function which creates file called x and writes Hello World there.

\$\endgroup\$
2
\$\begingroup\$

Node.js, 42 bytes

require("fs").writeFile('o','Hello World')

i don't think this needs explanation

Node.js REPL, 31 bytes

fs.writeFile('o','Hello World')

for some reason in repl you dont need to include fs

\$\endgroup\$
  • \$\begingroup\$ I don't think this works. In order for it to work, you'd need require("fs").writeFile("o","Hello World"). Otherwise, fs is not included. \$\endgroup\$ – Conor O'Brien Aug 1 '16 at 19:45
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ huh ok. works in REPL for some reason \$\endgroup\$ – Downgoat Aug 1 '16 at 19:49
2
\$\begingroup\$

C#, 93 77 76 bytes

using System.IO;namespace N{class C{static void M(){File.WriteAllText("f", "Hello World");}}}

class C{static void Main(){System.IO.File.WriteAllText("f", "Hello World");}}

class C{static void Main(){System.IO.File.WriteAllText("f","Hello World");}}

See it work, with an exception for unauthorized file access.

Changelog

Rev2

  • Removed unnecessary namespace
  • Changed function name to Main (because otherwise it won't be detected as main function)
  • Removed using directive (thanks Jean Lourenço)

Rev3

  • Removed space that sneaked in.

C# (without boilerplate), 47 bytes

void M(){File.WriteAllText("f","Hello World");}
\$\endgroup\$
  • \$\begingroup\$ You can save some bytes by removing the using and appending it directly to the method: System.IO.File.WriteAllText[...] \$\endgroup\$ – Jean Lourenço Aug 2 '16 at 11:26
  • \$\begingroup\$ @JeanLourenço Thanks. I had that originally and then changed it for reasons unknown. \$\endgroup\$ – Bart van Nierop Aug 2 '16 at 14:15
2
\$\begingroup\$

R, 38 36 35 bytes

sink(" ");cat("Hello World");sink()

I like how the created file has no name ! It's just .txt anything, in fact !

-2 bytes thanks to @PEAR remark !
-1 bytes thanks to @BartvanNierop !

This code will produce a file with no name.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is the '.txt' really necessary? File endings are just for the user. A single character might me enough. \$\endgroup\$ – PEAR Aug 1 '16 at 13:02
  • 1
    \$\begingroup\$ I don't know R, but could you not shave off another byte, as by @PEAR's suggestion, by simply naming the file "a"? \$\endgroup\$ – Bart van Nierop Aug 3 '16 at 5:35
2
\$\begingroup\$

Bash, 18 bytes

echo Hello World>a
\$\endgroup\$
1
\$\begingroup\$

Python, 34 bytes

open("h","w").write("Hello World")

Outputs to a file called h.

\$\endgroup\$
1
\$\begingroup\$

APLX, 15 bytes

'Hello World'⍈1

Creates an APL component file containing just one component; the desired string. It can be read back with:

      'Hello World'⍇1
Hello World
\$\endgroup\$
1
\$\begingroup\$

Gema, 28 characters

\A=@write{o;Hello World}@end

Sample run:

bash-4.3$ gema '\A=@write{o;Hello World}@end'

bash-4.3$ cat o
Hello World
\$\endgroup\$
1
\$\begingroup\$

Racket, 43 bytes

(display"Hello World"(open-output-file"f"))
\$\endgroup\$
1
\$\begingroup\$

Julia, 47 bytes

f=open("o","w");write(f,"Hello World");close(f)

I tried using writedlm, but it didn't work out.

\$\endgroup\$
1
\$\begingroup\$

C, 37 bytes

main(){system("echo Hello World>o");}
\$\endgroup\$
1
\$\begingroup\$

Perl 6,  27  23 bytes

'o'.IO.spurt: 'Hello World'
spurt 'o','Hello World'
\$\endgroup\$
1
\$\begingroup\$

Java 7, 100 95 bytes

void f()throws Exception{java.io.Writer p=new java.io.PrintWriter("x");p.print("Hello World");}

Or if you want to close the writer after using it (101 bytes):

void f()throws Exception{try(java.io.Writer p=new java.io.PrintWriter("x")){p.print("Hello World");}}

Ungolfed:

class Main{
  static void f() throws Exception{
    try(java.io.Writer p = new java.io.PrintWriter("x")){
      p.print("Hello World");
    }
  }

  public static void main(String[] a){
    try{
      f();
    } catch(Exception ex){
    }
  }
}

Usage:

java -jar Main.jar
\$\endgroup\$
  • \$\begingroup\$ Java is my favorite language but jesus christ it's hilarious how bad a golfing language it is haha \$\endgroup\$ – Shaun Wild Aug 1 '16 at 11:44
  • 4
    \$\begingroup\$ @AlanTuning Indeed. xD I work with Java at work, and it's fun to code-golf in Java. You will NEVER be able to win a code-golf challenge here using Java, but it's still fun to write the Java code as short as possible. \$\endgroup\$ – Kevin Cruijssen Aug 1 '16 at 12:02
  • 2
    \$\begingroup\$ well, you can just void f()throws Exception{new java.io.PrintWriter("x").print("Hello World");} \$\endgroup\$ – cliffroot Aug 1 '16 at 15:13
  • \$\begingroup\$ java.io.Writer p=new java.io.PrintWriter saves you some more characters. \$\endgroup\$ – Frozn Aug 1 '16 at 18:39
  • \$\begingroup\$ @cliffroot Hmm, I tried that initially, but it didn't seem to write anything to disk. Will try again tomorrow, currently I don't have an IDE and file I/O doesn't work in ideone. \$\endgroup\$ – Kevin Cruijssen Aug 1 '16 at 19:26
1
\$\begingroup\$

eacal, 26 bytes

write a string Hello World

This writes a string Hello World to file a, creating it if not present. Basically:

write <fileName> <exec>

and

string <params to convert to string>

Run the program as:

λ node eacal.js writeFile
\$\endgroup\$
1
\$\begingroup\$

J, 21 bytes

'Hello World'1!:3<'o'

This writes to a file o in the current directory, or, if not called from a file, in your j64-804 file. 1!:3 is the write foreign, and <'o' is the boxed filename (filenames need to be boxed). The LHS is the string to write.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 89 bytes

This code was tested in Chrome. In other browsers, the element must be in the document for the click method to work.

a=document.createElement("a");a.href="data:text/plain,Hello World";a.download=1;a.click()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.