Python 2.7 file read error - python-2.7

I am struggling with file access - I have my python 2.7 here:
C:\Python27
I created a txt file data.txt
and I am trying to get the shell to read and print values from it. Location of data.txt is in the same folder (C:\Python27\data.txt)
Every time I got the same error msg:
Traceback (most recent call last):
File "C:/Python27/FileReader.py", line 1, in <module>
infile = open('data.txt', 'r') # Open the file for reading.
IOError: [Errno 2] No such file or directory: 'data'

You need to enter the full filename data.txt instead of data only:
infile = open('data.txt', 'r')

Related

create file containing '/' in file name in python

how can I create a file in python if the filename contains '/'
url='https://www.udacity.com/cs101x/index.html'
f=open(url,'w')
f.write('123')
f.close()
above code produces an error as
Traceback (most recent call last):
File "9.py", line 2, in <module>
f=open(url,'w')
IOError: [Errno 22] invalid mode ('w') or filename:https://www.udacity.com/cs101x/index.html'
Use os.path.basename() to isolate the filename.
import os
url='https://www.udacity.com/cs101x/index.html'
filename = os.path.basename(url)
f=open(filename,'w')
f.write('123')
f.close()
This will create a file called index.html

When i tried to rename a txt file in pythonwin

I wrote a script to rename a txt file in pythonwin as below.
import os
os.rename("northrdge.txt","northrdgechg.txt")
the above text file is in the path:C:\Users\gmayil\Desktop\northrdge.txt.
after this when i run the script i met below error:
Can anyone please help me on this ?
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 433, in ImportFile
exec codeObj in __main__.__dict__
File "<auto import>", line 1, in <module>
File "C:\Python27\Tools\gopi\renamefile.py", line 2, in <module>
os.rename("northrdge.txt","northrdgechg.txt")
WindowsError: [Error 2] The system cannot find the file specified
Your python-file lies in "C:\Python27\Tools\gopi\renamefile.py", but you say your txt-file is in "C:\Users\gmayil\Desktop\". If you pass a relative path, python searches in its own directory, which is "C:\Python27\Tools\gopi\".
So all you have to do is giving the complete path:
os.rename("C:\Users\gmayil\Desktop\northrdge.txt", "C:\Users\gmayil\Desktop\northrdgechg.txt")

Unable to copy files of certain extension in Python using loop

I want to loop through the files present in Audio_files folder and check if it is raw file or wave file and put it in the respective directories.
Here is my code:
import shutil
import os
source = os.listdir("/home/GM/codec_implement/Audio_files/")
destination = "/home/GM/codec_implement/raw_files/"
destination2 = "/home/GM/codec_implement/raw_files/wave_files/"
for files in source:
if files.endswith(".wav"):
shutil.copy(files,destination)
elif files.endswith(".raw"):
shutil.copy(files,destination)
else:
print "Invalid format"
Please let me know where I am making mistake. Error i get is:
Traceback (most recent call last): File "./checkaudiofiles.py", line
12, in shutil.copy(files,destination) File
"/usr/lib/python2.7/shutil.py", line 119, in copy copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile with
open(src, 'rb') as fsrc: IOError: [Errno 2] No such file or directory:
'test_sound.wav'
The error means that the test_sound.wav file is not on that directory. These file might be removed between the two proces or the os.listdir() and the shutil.copy() don't list the files the same way.

Python Transfering files to and from the remote machine

i want to transfer a file local to server machine.
import os
import paramiko
local_path = "/home/e100075/python/ss.txt"
remote_path = "/home/developers/screenshots/"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username="username", password="password")
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp')
print "output", ssh_stdout.read()
#Reading output of the executed command
error = ssh_stderr.read()
#Reading the error stream of the executed command
print "err", error, len(error)
#Transfering files to and from the remote machine
sftp = ssh.open_sftp()
sftp.get(remote_path, local_path)
sftp.put(local_path, remote_path)
sftp.close()
ssh.close()
After run the program. I got the errors below
Traceback (most recent call last):
File "file_copy.py", line 21, in <module>
sftp.get(remote_path, local_path)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 606, in get
fr = self.file(remotepath, 'rb')
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 245, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 635, in _request
return self._read_response(num)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 682, in
_read_response
self._convert_status(msg)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 708, in
_convert_status
raise IOError(errno.ENOENT, text)
IOError: [Errno 2] No such file
If you know the answer. please let me know.
Thanks for reading.
If you want to put a file to the server you probably will need
sftp.put(local_path, remote_path)
not
sftp.get(remote_path, local_path)
You need to give file name also, otherwise it will throw errors
sftp.put('filename.txt','/home/user/performance/destination.txt')
where 'filename.txt' must be in current directory or else give full path

IOError: No such file or directory:

# pick up the file which needs to be processed
current_file = file_names[0]
print "Processing current file: " + current_file
key = bucket.get_key(current_file)
print "Processing key: " + str(key)
key.get_contents_to_filename(working_dir + "test_stats_temp.dat")
print "Current directory: ",outputdir
print "File to process:",current_file
Processing test output for: ds=2013-08-27
Processing current file: output/test_count_day/ds=2013-08-27/task_201308270934_0003_r_000000
Processing key: Key: hadoop.test.com,output/test_count_day/ds=2013-08-27/task_201308270934_0003_r_000000
Traceback (most recent call last):
File "queue_consumer.py", line 493, in <module>
test_process.load_test_cnv_stats_daily(datestring,working_dir,mysqlconn,s3_conn,test_output_bucket,test_output)
File "/home/sbr/aaa/test_process.py", line 46, in load_test_cnv_stats_daily
key.get_contents_to_filename(working_dir + "test_stats_temp.dat")
File "/usr/lib/python2.7/dist-packages/boto/s3/key.py", line 1275, in get_contents_to_filename
fp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/home/sbr/aaa/test_stats_temp.dat'
I got this error, when I fetched data to DB from S3 output. I'm confused here. How to handle this issue?
The error:
IOError: [Errno 2] No such file or directory: '/home/sbr/aaa/test_stats_temp.dat'
Indicates that the path set with working_dir does not exist. Creating the directory will fix it.