Tracy Azzopardi
Tracy Azzopardi

Reputation: 11

File handling in Python for beginners

Hello I am new to python and I am encountering this error :

C:\Users\Dylan Galea\Desktop\Modelling and CS>python file_handling.py

File "file_handling.py", line 4

np.savetxt(\Users\Dylan Galea\Desktop\Modelling and

CS\test.txt,twoDarray,delimeter='\t') ^ SyntaxError: unexpected character after line continuation character

my code is this :

import numpy as np

twoDarray =np.array([[1,2,3],[4,5,6]])
np.savetxt(\Users\Dylan Galea\Desktop\Modelling and CS\test.txt,twoDarray,delimeter='\t')

can anyone help please ?

Upvotes: 0

Views: 90

Answers (3)

Patrick Haugh
Patrick Haugh

Reputation: 61032

Your file name should be a string.

np.savetxt(r'\Users\Dylan Galea\Desktop\Modelling and CS\test.txt',twoDarray,delimeter='\t')

Upvotes: 0

Hugo G
Hugo G

Reputation: 16516

Hi and welcome to StackOverflow. Please use the tools StackOverflow provides to properly structure your post (e.g. mark code etc.) and make sure the indentation and newlines of the Python code is correct since it's part of the syntax.

Regarding the question it's probably an issue with your path which is not marked as string (must be enclosed in quotation marks) and contains backslashes, which are special escape characters in Python. Depending on your operating system (Mac OS, Windows, Linux etc.) you might need to use forward slashes or double(!) backward slashes.

Try this:

twoDarray = np.array([[1,2,3],[4,5,6]])
np.savetxt("/Users/Dylan Galea/Desktop/Modelling and CS/test.txt", twoDarray,delimeter='\t')

Upvotes: 0

Armen Avetisyan
Armen Avetisyan

Reputation: 1258

Please use the code-syntax of stackoverflow so we can read your code easier.

It seems like you spelled delimiter wrong.

Upvotes: 0

Related Questions