Find file directory after using os.expanduser (python) - python

my program creates a log file on the users desktop using:
file = open(os.path.expanduser("~/Desktop/Log.txt"), 'a')
But later I need to send the log file back to myself using MIME. To send a file it needs the the file directory but I do not know how to find that? I tried doing this:
filename='(os.path.expanduser("~/Desktop/Log.txt"), 'a')'
But this does not work. Is there another way to do this? If there is then please give an example since I am a beginner and don't really know what im doing.

Did you try this one? See if it works.
directory=os.path.dirname(os.path.expanduser("~/Desktop/Log.txt"))
if you want absolute path try
fullpath=os.path.abspath(os.path.expanduser("~/Desktop/Log.txt"))
more convenient functions here:
https://docs.python.org/3/library/os.path.html

Related

Sumo's not finding the file.net.xml when I try to open the file.sumocfg

I'm searching for a solution because I'm blocked and I don't understand why.
I'm a new user of Sumo, and to make my directory much clearer and less messy, I create a New Directory in the Directory where I put all files related to the sumocfg file I was working on (net.xml and rou.xml including sumocfg)
But now when I want to open the sumocfg file, it claims that the net.xml file is not found by searching it in the older directory it was.
Here is the error brought
Errorfile
And my problem is that he is searching the net.xml file in the path "Sumo/FirstIntersection.net.xml" instead of "Sumo/FirstIntersection/FirstIntersection.net.xml"
And I don't know why because when I open a simulation, I locate the good path.
Opening Simulation
I hope I was clear in describing the problem encountered.
Have some of you encountered the same issue?
Thanks in advance for your replies.
The sumocfg file is an XML file which you can modify with your favorite text editor. In your case it probably contains an absolute path to your network, that's why it does not work. You can simply change it to a relative path though. So instead of <net-file value="/full/path/to/FirstIntersection.net.xml"/> it should read <net-file value="FirstIntersection.net.xml"/>

reading and storing data into a python data structure from a csv

i am trying to figure out how to read my survey data but i keep getting this error. i feel like i am missing some things when i type out how to specifically find the file i need... and would a dictionary be the best data structure to use for this? the ultimate goal is: by using each voting method to determine a winner from the data that was collected. i plan on using if/else if statements...
Did you check your directory properly? The error says No such file or directory. Provide the full correct dir for your csv file and it should work fine.
In addition to providing a full file path, you can put the csv file with the python script under the same folder and try.

How to call files for .py program without having to define entire path

Signature_1=(input('Name.txt:'))#"/Users/Owner/Desktop/x.txt"
Edit: Overall I would just like to get the name of file "x". Is there a generic approach that I can use to extract just the file name from the path and store it under a separate variable to be called later?
The above line when ran in command prompt requires that I input the entire file path. The input will be used in the program later to title graphs, so I would like to exclude the entire path and just get the file name. Is there a simple way to do this command prompt? I am used to working in jupyter which is much smoother for these kind of tasks. Thank you in advance
you can drop your file to cmd, but it seems impossible to not put the entire path to open the file
I found out I can just move the files to my directory and call them by name.

Python: How do I get python to open files out of it's folder?

I made a program to open files windows can't but I have to put the file everywhere I want to use it. How can I just give it a path to use to find the file to read?
Basically how would I open a file out of a program's folder?
I would think that it would be...
file = open('C://Users/Name/Python33/file','r') But that isn't working.
Could someone give me example code?
Use command line arguments:
sys.argv[...]
You can do this either through command line arguments, as suggested by #qarma, or any other kind of I/O (taking raw_input, for instance). If you're unfamiliar with accessing files by paths in Python, you may find os.path useful.

How to write to a read-only file in Python

Is there a way to write to a read-only file in Python? I am trying to write a script which helps me add debug statements at the start of every function in a given file. But the issue I have is that before I run the script, I have to manually remove the read-only flag on the file. is there anyway I can write to read-only files without manually having to remove them? Any suggestions will be deeply appreciated. Thanks.
If the user that runs the script doesn't have permissions to write in a file, you can't edit it. Basically, you need to have the w permission to edit a file. See Linux file permissions for more information.
If you want to get rid of it, you should make the file writable directly, or try to change its chmod with the os module, if you have enough permission to do this:
>>> os.chmod('path_to/file', 0755)
Try os.chmod() before opening the file.

Categories