LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2017, 08:09 AM   #1
Dylan_Oscar
LQ Newbie
 
Registered: Nov 2017
Location: Sydney, Australia
Distribution: Ubuntu, Mint
Posts: 16

Rep: Reputation: Disabled
Path to new file in Python program on Linux


Hello,
I am posting this here to see if anyone knows about this problem, but as it is Python related, I will try Stack Overflow if I can't get help here. I am running this on Linux Mint, and part of my problem is getting the program to recognise the Linux file structure.
I am trying to tell my program to open a new file in write mode in a specific directory, but the program does not appear to recognise any instructions. I am consulting Learn Python the Hard Way, which has a section in Exercise 16 on writing files, however he simply supplies file names on the command line when running the program. I would like my program to create files on its own, setting the title to include the date of creation.
If I simply ask it to open a new file it states that the name 'new_file' is not defined.

Code:
a = open(new_file.py, 'w')
a.write("Hello World.")
a.close()
If I include an absolute path from the root directory, the terminal simply points at the '/' in front of 'home' and states 'invalid syntax'. The same goes for a relative path, using './'.
I have tried importing os as well. The code suggested on Stack Overflow for another problem was:

Code:
import os
directory = os.path.dirname(__file__)
filename = os.path.join(directory, new_file.py)
This also states that the name 'new_file' is not defined.
I saw a suggestion elsewhere about importing the glob module, but this did not look relevant to my issue.

What I am specifically trying to do is write the contents of a dictionary from my module to a separate file, however if I can simply figure out how to write any string from the module to a file, I will be happy with that for a start.
Any insight will be greatly appreciated.
Thanks.
 
Old 11-10-2017, 08:25 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
In most languages if you want to use a literal string you need to use quotes.
If you don't specify a path it will be created in the same directory where you ran the program.

Code:
a = open("new_file.py", "w")
 
1 members found this post helpful.
Old 11-10-2017, 08:40 AM   #3
Dylan_Oscar
LQ Newbie
 
Registered: Nov 2017
Location: Sydney, Australia
Distribution: Ubuntu, Mint
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thank you. That makes a lot of sense.
That solution worked in a basic program, but it is not working within the main module I am using. As that is an issue with my Python code, I think I will have to put a post in Stack Overflow to ask for assistance there.
Thank you for your prompt reply.
 
Old 11-10-2017, 11:33 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
We do have a Programming forum. I've asked the moderators to move this thread there.
 
Old 11-10-2017, 02:49 PM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: Slackware®
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-10-2017, 03:39 PM   #6
Dylan_Oscar
LQ Newbie
 
Registered: Nov 2017
Location: Sydney, Australia
Distribution: Ubuntu, Mint
Posts: 16

Original Poster
Rep: Reputation: Disabled
Wow, I didn’t know about this section. That’s great.
I solved the other problem too. I had a bug trying to write from within a class.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation of python and dependencies in customized path instead of standard path srinietrx Linux - Software 2 06-01-2015 09:19 AM
Installation of python and dependencies in customized path instead of standard path srinietrx Linux - Software 1 05-31-2015 02:38 AM
bash: Can I pipe the name of a file created by a program to a python script? SparceMatrix Programming 4 01-07-2012 11:38 AM
[SOLVED] Can a PATH statement be used with Python in Ubuntu 9.10 ( Debian linux ) ? dlemper Linux - Newbie 7 10-14-2010 09:44 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration