简体   繁体   中英

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 ?

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

It seems like you spelled delimiter wrong.

Hi and welcome to StackOverflow. Please use the tools StackOverflow provides to properly structure your post (eg 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')

您的文件名应为字符串。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM