0

I'm using PyCharm, and when I try to execute the statement from here:

!head -n5 {train_dataset_fp}

IDE complains that this is SyntaxError: invalid syntax and program never executes. I thought the entire tutorial on TensorFlow is in Python, but seems like this code from completely different language. Has anyone proceed successfully through the TensorFlow: Get Started tutorial?

TiredOfProgramming
  • 845
  • 2
  • 16
  • 41

2 Answers2

4

This is not a python command, this is a unix one, to launch the head program. You can use PyCharm to open a Terminal on your target machine, and type:

head -n5 {train_dataset_fp}

... replacing {train_dataset_fp} with the actual path to your dataset, which you obtained/printed in the previous step of the tutorial, c.f. lines:

train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),
                                           origin=train_dataset_url)

print("Local copy of the dataset file: {}".format(train_dataset_fp))
benjaminplanche
  • 14,689
  • 5
  • 57
  • 69
  • thanks for your response. Just tried your suggestion, getting message: 'head' is not recognized as an internal or external command, operable program or batch file. – TiredOfProgramming May 02 '18 at 16:47
  • well, that's why. If you really want to have a look at these first lines of your CSV file, you can find a Python solution here: https://stackoverflow.com/a/1767589/624547 – benjaminplanche May 02 '18 at 16:53
  • That's the reason I'm really tired of programming. Nowadays you can never trust any official tutorials, even well branded Google's TensorFlow. Thanks for the advise that head command would never work in windows environment. I wish organizers of the Get Started page will indicate that tutorial for Unix users. – TiredOfProgramming May 02 '18 at 16:58
0

Since you're on Windows, you need to use Windows commands to achieve what head would do. If you have Powershell installed, you can use the command gc. If you don't, here's a workaround to print the first 5 lines of file.txt, prefixed with the line number:

findstr /n ".*" file.txt | findstr /b "[1-5]:"

inspired by this answer. Basically it numbers all lines in the file and then picks the first five. Obviously pretty inefficient for large files though. Use the "!" prefix as needed.

Peter Szoldan
  • 4,792
  • 1
  • 14
  • 24